Memory leaks: Added support for a simple configuration file format. This is not supposed to become the official configuration format, it's just a temporary format that we can use to start with.

This commit is contained in:
Daniel Marjamäki 2012-05-26 18:10:12 +02:00
parent 200390e7ad
commit b8b16172b8
2 changed files with 43 additions and 5 deletions

View File

@ -28,7 +28,7 @@
#include "checknullpointer.h" // <- isUpper
#include <iostream>
#include <fstream>
//---------------------------------------------------------------------------
@ -85,8 +85,7 @@ void CheckLeakAutoVar::doubleDeallocationError(const Token *tok, const std::stri
void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::string &functionName)
{
if (_settings->experimental)
{
if (_settings->experimental) {
reportError(tok,
Severity::information,
"leakconfiguration",
@ -94,6 +93,39 @@ void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::string &fu
}
}
void CheckLeakAutoVar::parseConfigurationFile(const std::string &filename)
{
std::ifstream fin(filename.c_str());
if (!fin.is_open())
return;
std::string line;
while (std::getline(fin,line)) {
if (line.compare(0,4,"MEM ",0,4) == 0) {
std::string f1;
unsigned int type = 1;
std::string::size_type pos1 = line.find_first_not_of(" ", 4U);
while (pos1 < line.size()) {
const std::string::size_type pos2 = line.find(" ", pos1);
std::string f;
if (pos2 == std::string::npos)
f = line.substr(pos1);
else
f = line.substr(pos1, pos2-pos1);
if (f1.empty())
f1 = f;
if (f == ":")
type = 2;
else if (type == 1)
cfgalloc[f] = f1;
else if (type == 1)
cfgdealloc[f] = f1;
pos1 = line.find_first_not_of(" ", pos2);
}
}
}
}
void CheckLeakAutoVar::check()
{
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
@ -125,13 +157,13 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
const std::set<unsigned int> conditionalAlloc(varInfo->conditionalAlloc);
// Allocation functions. key = function name, value = allocation type
std::map<std::string, std::string> allocFunctions;
std::map<std::string, std::string> allocFunctions(cfgalloc);
allocFunctions["malloc"] = "malloc";
allocFunctions["strdup"] = "malloc";
allocFunctions["fopen"] = "fopen";
// Deallocation functions. key = function name, value = allocation type
std::map<std::string, std::string> deallocFunctions;
std::map<std::string, std::string> deallocFunctions(cfgdealloc);
deallocFunctions["free"] = "malloc";
deallocFunctions["fclose"] = "fopen";

View File

@ -77,11 +77,17 @@ public:
/** @brief Run checks against the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckLeakAutoVar checkLeakAutoVar(tokenizer, settings, errorLogger);
checkLeakAutoVar.parseConfigurationFile("cppcheck.cfg");
checkLeakAutoVar.check();
}
private:
std::map<std::string,std::string> cfgalloc;
std::map<std::string,std::string> cfgdealloc;
void parseConfigurationFile(const std::string &filename);
/** check for leaks in all scopes */
void check();