2012-05-26 08:53:46 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-01-03 12:14:58 +01:00
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
|
2012-05-26 08:53:46 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef checkleakautovarH
|
|
|
|
#define checkleakautovarH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2012-05-26 08:53:46 +02:00
|
|
|
#include "check.h"
|
|
|
|
|
2014-05-23 14:09:34 +02:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB VarInfo {
|
2012-05-26 08:53:46 +02:00
|
|
|
public:
|
2013-07-02 07:18:19 +02:00
|
|
|
std::map<unsigned int, int> alloctype;
|
2012-05-26 08:53:46 +02:00
|
|
|
std::map<unsigned int, std::string> possibleUsage;
|
|
|
|
std::set<unsigned int> conditionalAlloc;
|
2012-10-27 16:36:14 +02:00
|
|
|
std::set<unsigned int> referenced;
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void clear() {
|
2012-05-26 08:53:46 +02:00
|
|
|
alloctype.clear();
|
|
|
|
possibleUsage.clear();
|
|
|
|
conditionalAlloc.clear();
|
2012-10-27 16:36:14 +02:00
|
|
|
referenced.clear();
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void erase(unsigned int varid) {
|
2012-05-26 08:53:46 +02:00
|
|
|
alloctype.erase(varid);
|
|
|
|
possibleUsage.erase(varid);
|
|
|
|
conditionalAlloc.erase(varid);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void swap(VarInfo &other) {
|
2012-05-26 08:53:46 +02:00
|
|
|
alloctype.swap(other.alloctype);
|
|
|
|
possibleUsage.swap(other.possibleUsage);
|
|
|
|
conditionalAlloc.swap(other.conditionalAlloc);
|
2012-10-27 16:36:14 +02:00
|
|
|
referenced.swap(other.referenced);
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** set possible usage for all variables */
|
|
|
|
void possibleUsageAll(const std::string &functionName);
|
|
|
|
|
|
|
|
void print();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check for leaks
|
|
|
|
*/
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckLeakAutoVar : public Check {
|
2012-05-26 08:53:46 +02:00
|
|
|
public:
|
|
|
|
/** This constructor is used when registering the CheckLeakAutoVar */
|
2014-11-20 14:20:09 +01:00
|
|
|
CheckLeakAutoVar() : Check(myName()) {
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** This constructor is used when running checks. */
|
|
|
|
CheckLeakAutoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2014-11-20 14:20:09 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Run checks against the simplified token list */
|
2014-11-20 14:20:09 +01:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2012-05-26 08:53:46 +02:00
|
|
|
CheckLeakAutoVar checkLeakAutoVar(tokenizer, settings, errorLogger);
|
|
|
|
checkLeakAutoVar.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/** check for leaks in all scopes */
|
|
|
|
void check();
|
|
|
|
|
|
|
|
/** check for leaks in a function scope */
|
|
|
|
void checkScope(const Token * const startToken,
|
|
|
|
VarInfo *varInfo,
|
|
|
|
std::set<unsigned int> notzero);
|
|
|
|
|
|
|
|
/** parse function call */
|
2013-07-02 07:18:19 +02:00
|
|
|
void functionCall(const Token *tok, VarInfo *varInfo, const int dealloc);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
|
|
|
/** return. either "return" or end of variable scope is seen */
|
|
|
|
void ret(const Token *tok, const VarInfo &varInfo);
|
|
|
|
|
|
|
|
/** if variable is allocated then there is a leak */
|
|
|
|
void leakIfAllocated(const Token *vartok, const VarInfo &varInfo);
|
|
|
|
|
2013-07-02 07:18:19 +02:00
|
|
|
void leakError(const Token* tok, const std::string &varname, int type);
|
2012-05-26 08:53:46 +02:00
|
|
|
void mismatchError(const Token* tok, const std::string &varname);
|
|
|
|
void deallocUseError(const Token *tok, const std::string &varname);
|
2012-06-11 18:28:31 +02:00
|
|
|
void deallocReturnError(const Token *tok, const std::string &varname);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
|
|
|
/** message: user configuration is needed to complete analysis */
|
|
|
|
void configurationInfo(const Token* tok, const std::string &functionName);
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
2012-05-26 08:53:46 +02:00
|
|
|
CheckLeakAutoVar c(0, settings, errorLogger);
|
2012-06-30 16:23:10 +02:00
|
|
|
c.deallocReturnError(0, "p");
|
2012-05-26 08:53:46 +02:00
|
|
|
c.configurationInfo(0, "f"); // user configuration is needed to complete analysis
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2012-06-30 17:44:05 +02:00
|
|
|
return "Leaks (auto variables)";
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
std::string classInfo() const {
|
2012-08-26 16:22:46 +02:00
|
|
|
return "Detect when a auto variable is allocated but not deallocated.\n";
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkleakautovarH
|