forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextracttests.cpp
More file actions
162 lines (135 loc) · 4.91 KB
/
extracttests.cpp
File metadata and controls
162 lines (135 loc) · 4.91 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <fstream>
#include <sstream>
const std::string ext(".cpp");
static std::string str(unsigned int value)
{
std::ostringstream ostr;
ostr << value;
return ostr.str();
}
int main(const int argc, const char * const * const argv)
{
if (argc != 2) {
std::cerr << "syntax: extracttests testfile" << std::endl;
return 0;
}
std::string testname;
unsigned int subcount = 0;
std::ifstream f(argv[1]);
std::string line;
while (std::getline(f, line)) {
{
std::string::size_type pos = line.find_first_not_of(" ");
if (pos > 0 && pos != std::string::npos)
line.erase(0, pos);
}
if (line.compare(0, 5, "void ") == 0) {
testname = line.substr(5, line.size() - 7);
subcount = 0;
continue;
}
if (line == "}") {
testname = "";
subcount = 0;
continue;
}
if (!testname.empty() && line.compare(0, 5, "check") == 0 && line.find("(\"") != std::string::npos) {
std::ofstream fout((testname + str(++subcount) + ext).c_str());
fout << "#include <string.h>" << std::endl;
fout << "#include <stdio.h>" << std::endl;
fout << "#include <stdlib.h>" << std::endl;
if (testname == "nullpointer1") {
if (subcount < 6) {
fout << "class Token\n"
<< "{\n"
<< "public:\n"
<< " const char *str() const;\n"
<< " const Token *next() const;\n"
<< " unsigned int size() const;\n"
<< " char read () const;\n"
<< " operator bool() const;\n"
<< "};\n"
<< "static Token *tokens;\n";
} else {
fout << "struct A\n"
"{\n"
" char b();\n"
" A *next;\n"
"};\n";
}
}
if (testname == "nullpointer2") {
fout << "class Fred\n"
<< "{\n"
<< "public:\n"
<< " void hello() const;\n"
<< " operator bool() const;\n"
<< "};\n";
}
if (testname == "nullpointer3") {
fout << "struct DEF { };\n"
<< "struct ABC : public DEF\n"
<< "{\n"
<< " int a,b,c;\n"
<< " struct ABC *next;\n"
<< "};\n"
<< "void bar(int); void f(struct ABC **);\n";
}
if (testname == "nullpointer4") {
fout << "void bar(int);\n"
<< "int** f(int **p = 0);\n"
<< "extern int x;\n"
<< "struct P {\n"
<< " bool check() const;\n"
<< " P* next() const;\n"
<< "};\n";
}
if (testname == "nullpointer5") {
fout << "struct A {\n"
<< " char c() const;\n"
<< " operator bool() const;\n"
<< "};\n";
}
if (testname == "nullpointer6") {
fout << "struct Foo {\n"
<< " void abcd() const;\n"
<< "};\n"
<< "struct FooBar : public Foo { };\n"
<< "struct FooCar : public Foo { };\n"
<< "extern int a;\n";
}
if (testname == "nullpointer7") {
fout << "struct wxLongLong {\n"
<< " wxLongLong(int) { }\n"
<< " long GetValue() const;\n"
<< "};\n";
}
do {
std::string::size_type pos = line.find("\"");
if (pos == std::string::npos)
break;
line.erase(0, pos + 1);
pos = line.rfind("\"");
if (pos == std::string::npos)
break;
const bool lastline(line.find(");", pos) != std::string::npos);
line.erase(pos);
pos = 0;
while ((pos = line.find("\\", pos)) != std::string::npos) {
line.erase(pos, 1);
if (line[pos] == 'n')
line.erase(pos, 1);
else
++pos;
}
fout << line << std::endl;
if (lastline)
break;
} while (std::getline(f, line));
fout << std::endl;
continue;
}
}
return 0;
}