2012-12-31 12:41:36 +01:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "cppcheck.h"
|
|
|
|
|
|
|
|
|
|
|
|
class CppcheckExecutor : public ErrorLogger {
|
|
|
|
private:
|
|
|
|
CppCheck cppcheck;
|
2013-01-01 16:18:06 +01:00
|
|
|
std::string pattern;
|
2012-12-31 12:41:36 +01:00
|
|
|
bool foundLine;
|
|
|
|
|
|
|
|
public:
|
2013-01-01 16:18:06 +01:00
|
|
|
CppcheckExecutor(int linenr)
|
2012-12-31 12:41:36 +01:00
|
|
|
: ErrorLogger()
|
|
|
|
, cppcheck(*this,false)
|
|
|
|
, foundLine(false) {
|
2013-01-01 16:18:06 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << linenr;
|
|
|
|
pattern = ":" + ostr.str() + "]";
|
|
|
|
cppcheck.settings().addEnabled("all");
|
|
|
|
cppcheck.settings().inconclusive = true;
|
|
|
|
cppcheck.settings()._force = true;
|
2012-12-31 12:41:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool run(const char filename[]) {
|
|
|
|
foundLine = false;
|
|
|
|
cppcheck.check(filename);
|
|
|
|
return foundLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportOut(const std::string &outmsg) { }
|
|
|
|
void reportErr(const ErrorLogger::ErrorMessage &msg) {
|
2013-01-01 16:18:06 +01:00
|
|
|
if (msg.toString(false).find(pattern) != std::string::npos) {
|
2013-01-01 13:56:37 +01:00
|
|
|
foundLine = true;
|
|
|
|
cppcheck.terminate();
|
|
|
|
}
|
2012-12-31 12:41:36 +01:00
|
|
|
}
|
|
|
|
void reportProgress(const std::string &filename, const char stage[], const unsigned int value) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-01 16:18:06 +01:00
|
|
|
static bool test(const char *filename, int linenr, const std::vector<std::string> &filedata, const std::size_t line1, const std::size_t line2)
|
2012-12-31 12:41:36 +01:00
|
|
|
{
|
|
|
|
std::string path(filename);
|
|
|
|
if (path.find_first_of("\\/") != std::string::npos)
|
|
|
|
path = path.erase(1 + path.find_last_of("\\/"));
|
|
|
|
else
|
|
|
|
path.clear();
|
|
|
|
|
|
|
|
const std::string tempfilename(path + "__temp__" + std::strrchr(filename,'.'));
|
|
|
|
std::ofstream fout(tempfilename.c_str());
|
|
|
|
for (std::size_t i = 0; i < filedata.size(); i++)
|
|
|
|
fout << ((i>=line1 && i<=line2) ? "" : filedata[i]) << std::endl;
|
|
|
|
fout.close();
|
2013-01-01 16:18:06 +01:00
|
|
|
|
|
|
|
CppcheckExecutor cppcheck(linenr);
|
2012-12-31 12:41:36 +01:00
|
|
|
return cppcheck.run(tempfilename.c_str());
|
|
|
|
}
|
|
|
|
|
2013-01-01 16:18:06 +01:00
|
|
|
static bool test(const char *filename, int linenr, const std::vector<std::string> &filedata, const std::size_t line)
|
2012-12-31 12:41:36 +01:00
|
|
|
{
|
2013-01-01 16:18:06 +01:00
|
|
|
return test(filename, linenr, filedata, line, line);
|
2012-12-31 12:41:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printstr(const std::vector<std::string> &filedata, int i1, int i2)
|
|
|
|
{
|
|
|
|
std::cout << filedata.size();
|
|
|
|
for (int i = i1; i < i2; ++i)
|
|
|
|
std::cout << i << ":" << filedata[i] << std::endl;
|
|
|
|
}
|
|
|
|
|
2013-01-01 16:18:06 +01:00
|
|
|
static std::vector<std::string> readfile(const std::string &filename)
|
|
|
|
{
|
|
|
|
std::vector<std::string> filedata;
|
|
|
|
std::ifstream fin(filename.c_str());
|
|
|
|
std::string line;
|
|
|
|
bool blockComment = false;
|
|
|
|
while (std::getline(fin,line)) {
|
|
|
|
// replace various space characters with space
|
|
|
|
for (std::string::size_type pos = 0; pos < line.size(); ++pos) {
|
|
|
|
if (line[pos] & 0x80 || std::isspace(line[pos]))
|
|
|
|
line[pos] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove block comments TODO: Handle /* inside strings
|
|
|
|
if (blockComment) {
|
|
|
|
if (line.find("*/") == std::string::npos)
|
|
|
|
line.clear();
|
|
|
|
else {
|
|
|
|
if (line.find("*/") + 2U == line.size())
|
|
|
|
line.clear();
|
|
|
|
else
|
|
|
|
line = line.substr(line.find("*/") + 2U);
|
|
|
|
blockComment = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!blockComment && line.find("/*") != std::string::npos) {
|
|
|
|
std::string::size_type pos = line.find("/*");
|
|
|
|
if (line.find("*/",pos) == std::string::npos) {
|
|
|
|
blockComment = true;
|
|
|
|
if (pos==0)
|
|
|
|
line.clear();
|
|
|
|
else
|
|
|
|
line = line.substr(0,pos);
|
|
|
|
} else {
|
|
|
|
blockComment = false;
|
|
|
|
line = line.erase(pos, 2U + line.find("*/", pos) - pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove // comments
|
|
|
|
if (line.find("//") != std::string::npos)
|
|
|
|
line = line.substr(0, line.find("//"));
|
|
|
|
|
|
|
|
// empty line
|
|
|
|
if (line.find_first_not_of(" ") == std::string::npos)
|
|
|
|
line.clear();
|
|
|
|
else {
|
|
|
|
const std::string::size_type pos = line.find_first_not_of(" ");
|
|
|
|
|
|
|
|
// remove spaces before leading #
|
|
|
|
if (line[pos]=='#')
|
|
|
|
line = line.substr(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove trailing spaces
|
|
|
|
while (!line.empty() && std::isspace(line[line.size()-1U]))
|
|
|
|
line = line.substr(0, line.size() - 1U);
|
|
|
|
|
|
|
|
filedata.push_back(line);
|
|
|
|
}
|
|
|
|
return filedata;
|
|
|
|
}
|
|
|
|
|
2012-12-31 12:41:36 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
std::cout << "cppcheck tool that reduce code for a false positive" << std::endl;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
std::cerr << "Syntax: " << argv[0] << " filename line" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const filename = argv[1];
|
2013-01-01 16:18:06 +01:00
|
|
|
int linenr = std::atoi(argv[2]);
|
2012-12-31 12:41:36 +01:00
|
|
|
|
|
|
|
std::cout << "make sure false positive can be reproduced" << std::endl;
|
|
|
|
|
|
|
|
// Execute Cppcheck on the file..
|
|
|
|
{
|
2013-01-01 16:18:06 +01:00
|
|
|
CppcheckExecutor cppcheck(linenr);
|
|
|
|
if (!cppcheck.run(filename)) {
|
|
|
|
std::cerr << "Can't reproduce false positive at line " << linenr << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2012-12-31 12:41:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-01 16:18:06 +01:00
|
|
|
// Read file..
|
|
|
|
std::vector<std::string> filedata(readfile(filename));
|
2012-12-31 12:41:36 +01:00
|
|
|
|
|
|
|
// Write resulting code..
|
2013-01-01 16:18:06 +01:00
|
|
|
if (!test(filename, linenr, filedata, ~0)) {
|
2012-12-31 12:41:36 +01:00
|
|
|
std::cerr << "Cleanup failed." << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove includes..
|
2013-01-01 16:18:06 +01:00
|
|
|
std::set<std::string> headers;
|
2012-12-31 12:41:36 +01:00
|
|
|
for (std::size_t i = 0; i < filedata.size(); i++) {
|
|
|
|
if (filedata[i].compare(0,8,"#include")==0) {
|
2013-01-01 16:18:06 +01:00
|
|
|
if (test(filename, linenr, filedata, i)) {
|
|
|
|
std::cout << "removed #include : " << filedata[i] << std::endl;
|
2012-12-31 12:41:36 +01:00
|
|
|
filedata[i].clear();
|
|
|
|
} else {
|
2013-01-01 16:18:06 +01:00
|
|
|
std::string header = filedata[i];
|
|
|
|
header = header.substr(1U + header.find_first_of("\"<"));
|
|
|
|
header = header.erase(header.find_last_of("\">"));
|
|
|
|
if (headers.find(header) != headers.end()) {
|
|
|
|
std::cerr << "Failed to reduce headers" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
headers.insert(header);
|
|
|
|
std::string path(filename);
|
|
|
|
if (path.find_first_of("\\/") != std::string::npos)
|
|
|
|
path = path.erase(1 + path.find_last_of("\\/"));
|
|
|
|
else
|
|
|
|
path.clear();
|
|
|
|
std::cout << "expand #include : " << (path+header) << std::endl;
|
|
|
|
std::vector<std::string> data(readfile(path+header));
|
|
|
|
if (!data.empty()) {
|
|
|
|
filedata[i].clear();
|
|
|
|
filedata.insert(filedata.begin()+i, data.begin(), data.end());
|
|
|
|
linenr += data.size();
|
|
|
|
}
|
2012-12-31 12:41:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove blocks of code..
|
|
|
|
for (std::size_t i = 0; i < filedata.size(); i++) {
|
|
|
|
const std::string line = (i==0U&&filedata.empty()) ? std::string(";") : filedata[i];
|
|
|
|
if (line.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char startChar = line[0];
|
|
|
|
const char endChar = line[line.size() - 1U];
|
|
|
|
|
|
|
|
// some kind of single line declaration
|
|
|
|
if (std::isalpha(startChar) && endChar==';') {
|
2013-01-01 16:18:06 +01:00
|
|
|
if (test(filename, linenr, filedata, i)) {
|
2012-12-31 12:41:36 +01:00
|
|
|
filedata[i].clear();
|
|
|
|
std::cout << "removed declaration at line " << i << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "kept declaration at line " << i << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove a function body below a '}'
|
|
|
|
bool decl = bool(!std::isspace(startChar) && (endChar=='}' || endChar==';'));
|
|
|
|
while (decl) {
|
|
|
|
decl = false; // might be set to true below
|
|
|
|
std::size_t pos = ++i;
|
|
|
|
while (pos < filedata.size() && filedata[pos].empty())
|
|
|
|
++pos;
|
|
|
|
if ((pos+2U < filedata.size()) && (std::isalpha(filedata[pos].at(0)))) {
|
|
|
|
|
|
|
|
// does code block start with "{"
|
|
|
|
std::size_t pos2 = pos;
|
|
|
|
if (filedata[pos].find("(") != std::string::npos && filedata[pos].find(")")==std::string::npos) {
|
|
|
|
++pos2;
|
|
|
|
while (pos2+2U < filedata.size() && !filedata[pos2].empty() && filedata[pos2].find_first_of("(){}") == std::string::npos)
|
|
|
|
++pos2;
|
|
|
|
if (filedata[pos2].find_first_of("({}")!=std::string::npos || filedata[pos2].find(")") == std::string::npos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pos2+2U >= filedata.size() || filedata[pos2+1U] != "{")
|
|
|
|
break;
|
|
|
|
pos2 += 2;
|
|
|
|
|
|
|
|
// find end of block..
|
2013-01-01 14:33:32 +01:00
|
|
|
int level = 0;
|
|
|
|
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")) {
|
|
|
|
if (filedata[pos2].compare(0,3,"#if") == 0)
|
|
|
|
++level;
|
|
|
|
else if (filedata[pos2] == "#endif")
|
|
|
|
--level;
|
2012-12-31 12:41:36 +01:00
|
|
|
++pos2;
|
2013-01-01 14:33:32 +01:00
|
|
|
}
|
|
|
|
if (level != 0)
|
|
|
|
break;
|
2012-12-31 12:41:36 +01:00
|
|
|
|
|
|
|
// does block of code end with a '}'
|
|
|
|
if ((pos2 < filedata.size()) && (filedata[pos2] == "}" || filedata[pos2] == "};")) {
|
2013-01-01 16:18:06 +01:00
|
|
|
if (test(filename, linenr, filedata, pos, pos2)) {
|
2012-12-31 12:41:36 +01:00
|
|
|
for (i = pos; i <= pos2; i++)
|
|
|
|
filedata[i].clear();
|
|
|
|
std::cout << "removed block of code at lines " << pos << "-" << pos2 << std::endl;
|
|
|
|
decl = true;
|
|
|
|
} else {
|
|
|
|
std::cout << "kept block of code at lines " << pos << "-" << pos2 << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write resulting code..
|
|
|
|
{
|
|
|
|
const std::string outfilename(std::string("__out__") + std::strrchr(filename,'.'));
|
|
|
|
std::ofstream fout(outfilename.c_str());
|
|
|
|
for (std::size_t i = 0; i < filedata.size(); i++) {
|
|
|
|
if (!filedata[i].empty())
|
|
|
|
fout << filedata[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|