Skip to content

Commit 1f3628a

Browse files
committed
reduce: remove more code blocks
1 parent 28d5ad9 commit 1f3628a

1 file changed

Lines changed: 45 additions & 23 deletions

File tree

‎tools/reduce.cpp‎

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <cstring>
77

88
#include "cppcheck.h"
9-
9+
#include "mathlib.h"
1010

1111
class CppcheckExecutor : public ErrorLogger {
1212
private:
@@ -15,13 +15,11 @@ class CppcheckExecutor : public ErrorLogger {
1515
bool foundLine;
1616

1717
public:
18-
CppcheckExecutor(int linenr)
18+
CppcheckExecutor(std::size_t linenr)
1919
: ErrorLogger()
2020
, cppcheck(*this,false)
21+
, pattern(":" + MathLib::longToString(linenr) + "]")
2122
, foundLine(false) {
22-
std::ostringstream ostr;
23-
ostr << linenr;
24-
pattern = ":" + ostr.str() + "]";
2523
cppcheck.settings().addEnabled("all");
2624
cppcheck.settings().inconclusive = true;
2725
cppcheck.settings()._force = true;
@@ -33,18 +31,18 @@ class CppcheckExecutor : public ErrorLogger {
3331
return foundLine;
3432
}
3533

36-
void reportOut(const std::string &outmsg) { }
34+
void reportOut(const std::string &/*outmsg*/) { }
3735
void reportErr(const ErrorLogger::ErrorMessage &msg) {
3836
if (msg.toString(false).find(pattern) != std::string::npos) {
3937
foundLine = true;
4038
cppcheck.terminate();
4139
}
4240
}
43-
void reportProgress(const std::string &filename, const char stage[], const unsigned int value) { }
41+
void reportProgress(const std::string & /*filename*/, const char /*stage*/[], const unsigned int /*value*/) { }
4442
};
4543

4644

47-
static bool test(const char *filename, int linenr, const std::vector<std::string> &filedata, const std::size_t line1, const std::size_t line2)
45+
static bool test(const char *filename, std::size_t linenr, const std::vector<std::string> &filedata, const std::size_t line1, const std::size_t line2)
4846
{
4947
std::string path(filename);
5048
if (path.find_first_of("\\/") != std::string::npos)
@@ -62,7 +60,7 @@ static bool test(const char *filename, int linenr, const std::vector<std::string
6260
return cppcheck.run(tempfilename.c_str());
6361
}
6462

65-
static bool test(const char *filename, int linenr, const std::vector<std::string> &filedata, const std::size_t line)
63+
static bool test(const char *filename, std::size_t linenr, const std::vector<std::string> &filedata, const std::size_t line)
6664
{
6765
return test(filename, linenr, filedata, line, line);
6866
}
@@ -137,7 +135,7 @@ static std::vector<std::string> readfile(const std::string &filename)
137135
return filedata;
138136
}
139137

140-
static bool removeMacrosInGlobalScope(std::vector<std::string> &filedata, const char filename[], const int linenr)
138+
static bool removeMacrosInGlobalScope(std::vector<std::string> &filedata, const char filename[], const std::size_t linenr)
141139
{
142140
bool changed = false;
143141

@@ -191,7 +189,7 @@ static bool removeMacrosInGlobalScope(std::vector<std::string> &filedata, const
191189
return changed;
192190
}
193191

194-
static bool removeBlocksOfCode(std::vector<std::string> &filedata, const char filename[], const int linenr)
192+
static bool removeBlocksOfCode(std::vector<std::string> &filedata, const char filename[], const std::size_t linenr)
195193
{
196194
bool changed = false;
197195

@@ -239,7 +237,7 @@ static bool removeBlocksOfCode(std::vector<std::string> &filedata, const char fi
239237

240238
// find end of block..
241239
int level = 0;
242-
while ((pos2 < filedata.size()) && (filedata[pos2].empty() || std::isspace(filedata[pos2].at(0)) || filedata[pos2].compare(0,3,"#if")==0 || filedata[pos2]=="#else" || filedata[pos2]=="#endif")) {
240+
while ((pos2 < filedata.size()) && (filedata[pos2].empty() || std::isspace(filedata[pos2].at(0)) || (std::isalpha(filedata[pos2].at(0)) && filedata[pos2].at(filedata[pos2].size()-1U) == ':') || filedata[pos2].compare(0,3,"#if")==0 || filedata[pos2]=="#else" || filedata[pos2]=="#endif")) {
243241
if (filedata[pos2].compare(0,3,"#if") == 0)
244242
++level;
245243
else if (filedata[pos2] == "#endif")
@@ -267,7 +265,7 @@ static bool removeBlocksOfCode(std::vector<std::string> &filedata, const char fi
267265
return changed;
268266
}
269267

270-
static bool removeClassAndStructMembers(std::vector<std::string> &filedata, const char filename[], const int linenr)
268+
static bool removeClassAndStructMembers(std::vector<std::string> &filedata, const char filename[], const std::size_t linenr)
271269
{
272270
bool changed = false;
273271

@@ -303,16 +301,22 @@ static bool removeClassAndStructMembers(std::vector<std::string> &filedata, cons
303301
}
304302
}
305303

306-
if (line[0] != '#')
307-
decl = bool(endChar == ';' || endChar == '}');
304+
if (line[0] != '#') {
305+
if (decl && std::isalpha(line[0]) && endChar == ':') {
306+
decl = true;
307+
for (std::string::size_type linepos = 0U; linepos+1U < line.size(); ++linepos)
308+
decl &= (std::isspace(line[linepos]) || std::isalpha(line[linepos]));
309+
} else
310+
decl = bool(endChar == ';' || endChar == '}');
311+
}
308312
}
309313
}
310314
}
311315

312316
return changed;
313317
}
314318

315-
static bool removeIfEndIf(std::vector<std::string> &filedata, const char filename[], const int linenr)
319+
static bool removeIfEndIf(std::vector<std::string> &filedata, const char filename[], const std::size_t linenr)
316320
{
317321
bool changed = false;
318322

@@ -345,13 +349,28 @@ int main(int argc, char *argv[])
345349
{
346350
std::cout << "cppcheck tool that reduce code for a false positive" << std::endl;
347351

348-
if (argc != 3) {
349-
std::cerr << "Syntax: " << argv[0] << " filename line" << std::endl;
350-
return EXIT_FAILURE;
352+
bool stdout = false;
353+
const char *filename = NULL;
354+
std::size_t linenr = 0;
355+
356+
for (int i = 1; i < argc; i++) {
357+
if (strcmp(argv[i], "--stdout") == 0)
358+
stdout = true;
359+
else if (filename==NULL && strchr(argv[i],'.'))
360+
filename = argv[i];
361+
else if (linenr == 0U && MathLib::isInt(argv[i]))
362+
linenr = std::atoi(argv[i]);
363+
else {
364+
std::cerr << "invalid option " << argv[i] << std::endl;
365+
return EXIT_FAILURE;
366+
}
351367
}
352368

353-
const char * const filename = argv[1];
354-
int linenr = std::atoi(argv[2]);
369+
if (linenr == 0U || filename == NULL) {
370+
std::cerr << "Syntax:" << std::endl
371+
<< argv[0] << " [--stdout] filename linenr" << std::endl;
372+
return EXIT_FAILURE;
373+
}
355374

356375
std::cout << "make sure false positive can be reproduced" << std::endl;
357376

@@ -417,10 +436,13 @@ int main(int argc, char *argv[])
417436
// Write resulting code..
418437
{
419438
const std::string outfilename(std::string("__out__") + std::strrchr(filename,'.'));
420-
std::ofstream fout(outfilename.c_str());
439+
std::ofstream fout;
440+
if (!stdout)
441+
fout.open(outfilename.c_str());
442+
std::ostream &os = stdout ? std::cout : fout;
421443
for (std::size_t i = 0; i < filedata.size(); i++) {
422444
if (!filedata[i].empty())
423-
fout << filedata[i] << std::endl;
445+
os << filedata[i] << std::endl;
424446
}
425447
}
426448

0 commit comments

Comments
 (0)