From b8b16172b88659c0f717c83b61ad444f7c19e43d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 May 2012 18:10:12 +0200 Subject: [PATCH] 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. --- lib/checkleakautovar.cpp | 42 +++++++++++++++++++++++++++++++++++----- lib/checkleakautovar.h | 6 ++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index c1a0ea874..2858ac561 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -28,7 +28,7 @@ #include "checknullpointer.h" // <- isUpper -#include +#include //--------------------------------------------------------------------------- @@ -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 conditionalAlloc(varInfo->conditionalAlloc); // Allocation functions. key = function name, value = allocation type - std::map allocFunctions; + std::map allocFunctions(cfgalloc); allocFunctions["malloc"] = "malloc"; allocFunctions["strdup"] = "malloc"; allocFunctions["fopen"] = "fopen"; // Deallocation functions. key = function name, value = allocation type - std::map deallocFunctions; + std::map deallocFunctions(cfgdealloc); deallocFunctions["free"] = "malloc"; deallocFunctions["fclose"] = "fopen"; diff --git a/lib/checkleakautovar.h b/lib/checkleakautovar.h index c7698c5f5..949b11f65 100644 --- a/lib/checkleakautovar.h +++ b/lib/checkleakautovar.h @@ -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 cfgalloc; + std::map cfgdealloc; + + void parseConfigurationFile(const std::string &filename); + /** check for leaks in all scopes */ void check();