2010-01-16 08:47:46 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
|
2010-01-16 08:47:46 +01: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 CheckClassH
|
|
|
|
#define CheckClassH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
|
|
|
#include "settings.h"
|
2010-11-13 07:31:56 +01:00
|
|
|
#include "symboldatabase.h"
|
2010-01-16 08:47:46 +01:00
|
|
|
|
|
|
|
class Token;
|
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief %Check classes. Uninitialized member variables, non-conforming operators, missing virtual destructor, etc */
|
2011-10-13 20:53:06 +02:00
|
|
|
class CheckClass : public Check {
|
2010-01-16 08:47:46 +01:00
|
|
|
public:
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief This constructor is used when registering the CheckClass */
|
2011-02-02 10:29:10 +01:00
|
|
|
CheckClass() : Check(myName()), symbolDatabase(NULL)
|
2010-01-16 08:47:46 +01:00
|
|
|
{ }
|
|
|
|
|
2010-03-17 22:16:18 +01:00
|
|
|
/** @brief This constructor is used when running checks. */
|
2010-07-26 16:46:37 +02:00
|
|
|
CheckClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger);
|
|
|
|
|
2010-03-13 21:42:59 +01:00
|
|
|
/** @brief Run checks on the normal token list */
|
2011-10-13 20:53:06 +02:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2010-01-16 08:47:46 +01:00
|
|
|
CheckClass checkClass(tokenizer, settings, errorLogger);
|
2010-03-13 21:42:59 +01:00
|
|
|
|
|
|
|
// can't be a simplified check .. the 'sizeof' is used.
|
2010-01-16 08:47:46 +01:00
|
|
|
checkClass.noMemset();
|
|
|
|
}
|
|
|
|
|
2010-03-13 21:42:59 +01:00
|
|
|
/** @brief Run checks on the simplified token list */
|
2011-10-13 20:53:06 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2010-01-16 08:47:46 +01:00
|
|
|
CheckClass checkClass(tokenizer, settings, errorLogger);
|
|
|
|
|
2010-04-21 08:38:25 +02:00
|
|
|
// Coding style checks
|
|
|
|
checkClass.constructors();
|
|
|
|
checkClass.operatorEq();
|
|
|
|
checkClass.privateFunctions();
|
|
|
|
checkClass.operatorEqRetRefThis();
|
|
|
|
checkClass.thisSubtraction();
|
|
|
|
checkClass.operatorEqToSelf();
|
2011-09-28 03:07:37 +02:00
|
|
|
checkClass.initializerList();
|
2010-04-21 08:38:25 +02:00
|
|
|
|
2010-01-16 08:47:46 +01:00
|
|
|
checkClass.virtualDestructor();
|
2010-01-23 09:38:35 +01:00
|
|
|
checkClass.checkConst();
|
2010-01-16 08:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief %Check that all class constructors are ok */
|
2010-01-16 08:47:46 +01:00
|
|
|
void constructors();
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief %Check that all private functions are called */
|
2010-01-16 08:47:46 +01:00
|
|
|
void privateFunctions();
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/**
|
|
|
|
* @brief %Check that the memsets are valid.
|
2010-03-13 21:42:59 +01:00
|
|
|
* The 'memset' function can do dangerous things if used wrong. If it
|
|
|
|
* is used on STL containers for instance it will clear all its data
|
|
|
|
* and then the STL container may leak memory or worse have an invalid state.
|
|
|
|
* It can also overwrite the virtual table.
|
2010-01-22 19:29:24 +01:00
|
|
|
* Important: The checking doesn't work on simplified tokens list.
|
|
|
|
*/
|
2010-01-16 08:47:46 +01:00
|
|
|
void noMemset();
|
2011-03-23 02:24:28 +01:00
|
|
|
void checkMemsetType(const Scope *start, const Token *tok, const Scope *type);
|
2010-01-16 08:47:46 +01:00
|
|
|
|
2010-09-11 08:23:30 +02:00
|
|
|
/** @brief 'operator=' should return something and it should not be const. */
|
2010-01-22 19:29:24 +01:00
|
|
|
void operatorEq();
|
2010-01-16 08:47:46 +01:00
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief 'operator=' should return reference to *this */
|
2010-01-16 08:47:46 +01:00
|
|
|
void operatorEqRetRefThis(); // Warning upon no "return *this;"
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief 'operator=' should check for assignment to self */
|
2010-01-16 08:47:46 +01:00
|
|
|
void operatorEqToSelf(); // Warning upon no check for assignment to self
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief The destructor in a base class should be virtual */
|
2010-01-16 08:47:46 +01:00
|
|
|
void virtualDestructor();
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief warn for "this-x". The indented code may be "this->x" */
|
2010-01-16 08:47:46 +01:00
|
|
|
void thisSubtraction();
|
2010-01-23 09:19:22 +01:00
|
|
|
|
|
|
|
/** @brief can member function be const? */
|
|
|
|
void checkConst();
|
2010-01-22 19:29:24 +01:00
|
|
|
|
2011-09-28 03:07:37 +02:00
|
|
|
/** @brief Check initializer list order */
|
|
|
|
void initializerList();
|
|
|
|
|
2010-08-07 12:26:42 +02:00
|
|
|
private:
|
2010-08-07 12:41:11 +02:00
|
|
|
/**
|
|
|
|
* @brief Create symbol database. For performance reasons, only call
|
|
|
|
* it if it's needed.
|
|
|
|
*/
|
|
|
|
void createSymbolDatabase();
|
|
|
|
|
2011-01-16 18:13:54 +01:00
|
|
|
const SymbolDatabase *symbolDatabase;
|
2010-07-18 10:18:41 +02:00
|
|
|
|
2010-01-16 08:47:46 +01:00
|
|
|
// Reporting errors..
|
|
|
|
void noConstructorError(const Token *tok, const std::string &classname, bool isStruct);
|
2010-05-16 14:43:42 +02:00
|
|
|
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname);
|
2010-01-16 08:47:46 +01:00
|
|
|
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname);
|
|
|
|
void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname);
|
2011-03-10 08:19:31 +01:00
|
|
|
void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type);
|
2011-04-01 01:40:28 +02:00
|
|
|
void operatorEqReturnError(const Token *tok, const std::string &className);
|
2010-01-16 08:47:46 +01:00
|
|
|
void virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived);
|
|
|
|
void thisSubtractionError(const Token *tok);
|
|
|
|
void operatorEqRetRefThisError(const Token *tok);
|
|
|
|
void operatorEqToSelfError(const Token *tok);
|
2010-01-23 09:19:22 +01:00
|
|
|
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname);
|
2010-03-10 07:47:01 +01:00
|
|
|
void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname);
|
2011-09-28 03:07:37 +02:00
|
|
|
void initializerListError(const Token *tok1,const Token *tok2, const std::string & classname, const std::string &varname);
|
2010-01-23 09:19:22 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
|
2010-12-29 12:43:29 +01:00
|
|
|
CheckClass c(0, settings, errorLogger);
|
|
|
|
c.noConstructorError(0, "classname", false);
|
|
|
|
c.uninitVarError(0, "classname", "varname");
|
|
|
|
c.operatorEqVarError(0, "classname", "");
|
|
|
|
c.unusedPrivateFunctionError(0, "classname", "funcname");
|
2011-03-10 08:19:31 +01:00
|
|
|
c.memsetError(0, "memfunc", "classname", "class");
|
2011-04-01 01:40:28 +02:00
|
|
|
c.operatorEqReturnError(0, "class");
|
2010-12-29 12:43:29 +01:00
|
|
|
//c.virtualDestructorError(0, "Base", "Derived");
|
|
|
|
c.thisSubtractionError(0);
|
|
|
|
c.operatorEqRetRefThisError(0);
|
|
|
|
c.operatorEqToSelfError(0);
|
|
|
|
c.checkConstError(0, "class", "function");
|
2011-09-28 03:07:37 +02:00
|
|
|
c.initializerListError(0, 0, "class", "variable");
|
2010-01-16 08:47:46 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
std::string myName() const {
|
2010-01-16 08:47:46 +01:00
|
|
|
return "Class";
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
std::string classInfo() const {
|
2010-01-16 08:47:46 +01:00
|
|
|
return "Check the code for each class.\n"
|
2010-01-17 14:56:56 +01:00
|
|
|
"* Missing constructors\n"
|
|
|
|
"* Are all variables initialized by the constructors?\n"
|
|
|
|
"* [[CheckMemset|Warn if memset, memcpy etc are used on a class]]\n"
|
2010-09-18 19:03:15 +02:00
|
|
|
//"* If it's a base class, check that the destructor is virtual\n"
|
2010-01-23 09:19:22 +01:00
|
|
|
"* Are there unused private functions\n"
|
2010-01-27 19:25:48 +01:00
|
|
|
"* 'operator=' should return reference to self\n"
|
|
|
|
"* 'operator=' should check for assignment to self\n"
|
2011-11-02 17:12:46 +01:00
|
|
|
"* Constness for member functions\n";
|
2010-01-16 08:47:46 +01:00
|
|
|
}
|
2010-09-11 08:23:30 +02:00
|
|
|
|
2010-11-14 06:50:33 +01:00
|
|
|
// operatorEqRetRefThis helper function
|
2011-01-17 18:29:19 +01:00
|
|
|
void checkReturnPtrThis(const Scope *scope, const Function *func, const Token *tok, const Token *last);
|
2010-11-14 06:50:33 +01:00
|
|
|
|
|
|
|
// operatorEqToSelf helper functions
|
2011-04-01 01:53:35 +02:00
|
|
|
bool hasDeallocation(const Function *func);
|
|
|
|
bool hasAssignSelf(const Function *func, const Token *rhs);
|
2010-11-14 06:50:33 +01:00
|
|
|
|
2011-01-16 11:18:12 +01:00
|
|
|
// checkConst helper functions
|
2011-01-17 18:29:19 +01:00
|
|
|
bool isMemberVar(const Scope *scope, const Token *tok);
|
|
|
|
bool isConstMemberFunc(const Scope *scope, const Token *tok);
|
|
|
|
bool checkConstFunc(const Scope *scope, const Token *tok);
|
2011-01-16 11:18:12 +01:00
|
|
|
/** @brief check if this function is virtual in the base classes */
|
2011-01-17 18:29:19 +01:00
|
|
|
bool isVirtualFunc(const Scope *scope, const Token *functionToken) const;
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
// constructors helper function
|
|
|
|
/** @brief Information about a member variable. Used when checking for uninitialized variables */
|
2011-10-13 20:53:06 +02:00
|
|
|
struct Usage {
|
2011-01-16 16:37:11 +01:00
|
|
|
Usage() : assign(false), init(false) { }
|
|
|
|
|
|
|
|
/** @brief has this variable been assigned? */
|
|
|
|
bool assign;
|
|
|
|
|
|
|
|
/** @brief has this variable been initialized? */
|
|
|
|
bool init;
|
|
|
|
};
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
bool isBaseClassFunc(const Token *tok, const Scope *scope);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief assign a variable in the varlist
|
|
|
|
* @param varname name of variable to mark assigned
|
2011-01-17 18:29:19 +01:00
|
|
|
* @param scope pointer to variable Scope
|
2011-01-16 16:37:11 +01:00
|
|
|
* @param usage reference to usage vector
|
|
|
|
*/
|
2011-01-17 18:29:19 +01:00
|
|
|
void assignVar(const std::string &varname, const Scope *scope, std::vector<Usage> &usage);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief initialize a variable in the varlist
|
|
|
|
* @param varname name of variable to mark initialized
|
2011-01-17 18:29:19 +01:00
|
|
|
* @param scope pointer to variable Scope
|
2011-01-16 16:37:11 +01:00
|
|
|
* @param usage reference to usage vector
|
|
|
|
*/
|
2011-01-17 18:29:19 +01:00
|
|
|
void initVar(const std::string &varname, const Scope *scope, std::vector<Usage> &usage);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief set all variables in list assigned
|
|
|
|
* @param usage reference to usage vector
|
|
|
|
*/
|
2011-12-13 00:24:34 +01:00
|
|
|
void assignAllVar(std::vector<Usage> &usage) const;
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief set all variables in list not assigned and not initialized
|
|
|
|
* @param usage reference to usage vector
|
|
|
|
*/
|
2011-12-13 00:24:34 +01:00
|
|
|
void clearAllVar(std::vector<Usage> &usage) const;
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a scope for a constructor or member function and set the "init" flags in the provided varlist
|
|
|
|
* @param func reference to the function that should be checked
|
|
|
|
* @param callstack the function doesn't look into recursive function calls.
|
2011-01-17 18:29:19 +01:00
|
|
|
* @param scope pointer to variable Scope
|
2011-01-16 16:37:11 +01:00
|
|
|
* @param usage reference to usage vector
|
|
|
|
*/
|
2011-01-17 18:29:19 +01:00
|
|
|
void initializeVarList(const Function &func, std::list<std::string> &callstack, const Scope *scope, std::vector<Usage> &usage);
|
2011-01-18 07:34:11 +01:00
|
|
|
|
|
|
|
bool canNotCopy(const Scope *scope) const;
|
2010-01-16 08:47:46 +01:00
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|
|
|
|
|