added internal support for handling list of classes that are automaticly deallocated

This commit is contained in:
Daniel Marjamäki 2009-03-06 07:22:07 +01:00
parent 28475b2986
commit d7cd09cb6e
4 changed files with 53 additions and 12 deletions

View File

@ -428,9 +428,9 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (_settings._showAll)
{
if (_settings._vcl && Token::Match(tok->tokAt(4), "( %var%") && (tok->strAt(3)[0] == 'T'))
if (_settings.isAutoDealloc(tok->strAt(3)))
{
// Guess this is a VCL class with automatic deallocation
// This class has automatic deallocation
alloc = No;
}
else
@ -1440,8 +1440,8 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass(const Token *
// Declaring member variable.. check allocations and deallocations
if (Token::Match(tok->next(), "%type% * %var% ;"))
{
// No false positives for vcl classes..
if (_settings._vcl && tok->next()->str()[0] == 'T')
// No false positives for auto deallocated classes..
if (_settings.isAutoDealloc(tok->strAt(1)))
continue;
if (tok->isName() || Token::Match(tok, "[;}]"))

View File

@ -19,6 +19,8 @@
#include "settings.h"
#include <algorithm>
Settings::Settings()
{
_debug = false;
@ -30,7 +32,6 @@ Settings::Settings()
_xml = false;
_unusedFunctions = false;
_security = false;
_vcl = false;
_jobs = 1;
_exitCode = 0;
}
@ -39,3 +40,25 @@ Settings::~Settings()
{
}
void Settings::autoDealloc(std::istream &istr)
{
std::string line;
while (getline(istr,line))
{
// Check if line has a valid classname..
if (line.empty())
continue;
// Add classname to list
_autoDealloc.push_back(line);
}
}
bool Settings::isAutoDealloc(const char classname[]) const
{
return (std::find(_autoDealloc.begin(), _autoDealloc.end(), classname) != _autoDealloc.end());
}

View File

@ -20,6 +20,10 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <list>
#include <string>
#include <istream>
/**
* This is just a container for general settings so that we don't need
* to pass individual values to functions or constructors now or in the
@ -27,6 +31,10 @@
*/
class Settings
{
private:
/** classes that are automaticly deallocated */
std::list<std::string> _autoDealloc;
public:
Settings();
virtual ~Settings();
@ -49,9 +57,6 @@ public:
/** Security checks */
bool _security;
/** Disable warnings for VCL classes */
bool _vcl;
/** How many processes/threads should do checking at the same
time. Default is 1. */
unsigned int _jobs;
@ -59,6 +64,12 @@ public:
/** If errors are found, this value is returned from main().
Default value is 0. */
int _exitCode;
/** Fill list of automaticly deallocated classes */
void autoDealloc(std::istream &istr);
/** is a class automaticly deallocated? */
bool isAutoDealloc(const char classname[]) const;
};
#endif // SETTINGS_H

View File

@ -1931,12 +1931,14 @@ private:
void checkvcl(const char code[])
void checkvcl(const char code[], const char _autoDealloc[])
{
// Tokenize..
Tokenizer tokenizer;
{
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
}
tokenizer.setVarId();
tokenizer.simplifyTokenList();
@ -1947,7 +1949,12 @@ private:
Settings settings;
settings._debug = true;
settings._showAll = true;
settings._vcl = true;
{
std::istringstream istr(_autoDealloc);
settings.autoDealloc(istr);
}
CheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this);
checkMemoryLeak.CheckMemoryLeak();
}
@ -1959,7 +1966,7 @@ private:
checkvcl("void Form1::foo()\n"
"{\n"
" TEdit *Edit1 = new TEdit(this);\n"
"}\n");
"}\n", "TEdit\n");
ASSERT_EQUALS("", errout.str());
}
@ -1977,7 +1984,7 @@ private:
"Fred::Fred()\n"
"{\n"
" button = new TButton(this);\n"
"}\n");
"}\n", "TButton\n");
ASSERT_EQUALS("", errout.str());
}
};