2010-01-16 08:47:46 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2020-05-10 11:16:32 +02:00
|
|
|
* Copyright (C) 2007-2020 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checkclassH
|
|
|
|
#define checkclassH
|
2010-01-16 08:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "tokenize.h"
|
2020-08-05 13:57:40 +02:00
|
|
|
#include "symboldatabase.h"
|
2010-01-16 08:47:46 +01:00
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class ErrorLogger;
|
2011-12-23 22:31:48 +01:00
|
|
|
class Function;
|
2017-05-27 04:33:47 +02:00
|
|
|
class Scope;
|
|
|
|
class Settings;
|
|
|
|
class SymbolDatabase;
|
|
|
|
class Token;
|
2020-12-21 07:14:52 +01:00
|
|
|
class Type;
|
2010-01-16 08:47:46 +01:00
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
2010-01-22 19:29:24 +01:00
|
|
|
/** @brief %Check classes. Uninitialized member variables, non-conforming operators, missing virtual destructor, etc */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB 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 */
|
2018-06-17 18:43:54 +02:00
|
|
|
CheckClass() : Check(myName()), mSymbolDatabase(nullptr) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
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 */
|
2019-01-12 07:37:42 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
|
2012-09-10 19:02:32 +02:00
|
|
|
if (tokenizer->isC())
|
|
|
|
return;
|
|
|
|
|
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.
|
2013-11-18 16:56:00 +01:00
|
|
|
checkClass.checkMemset();
|
2010-04-21 08:38:25 +02:00
|
|
|
checkClass.constructors();
|
|
|
|
checkClass.privateFunctions();
|
|
|
|
checkClass.operatorEqRetRefThis();
|
|
|
|
checkClass.thisSubtraction();
|
|
|
|
checkClass.operatorEqToSelf();
|
2012-05-18 16:54:58 +02:00
|
|
|
checkClass.initializerListOrder();
|
|
|
|
checkClass.initializationListUsage();
|
2014-08-05 11:48:53 +02:00
|
|
|
checkClass.checkSelfInitialization();
|
2010-01-16 08:47:46 +01:00
|
|
|
checkClass.virtualDestructor();
|
2010-01-23 09:38:35 +01:00
|
|
|
checkClass.checkConst();
|
2012-09-17 17:59:35 +02:00
|
|
|
checkClass.copyconstructors();
|
2020-02-14 17:10:12 +01:00
|
|
|
// FIXME: Only report warnings for inherited classes
|
|
|
|
// checkClass.checkVirtualFunctionCallInConstructor();
|
2013-08-20 06:29:19 +02:00
|
|
|
checkClass.checkDuplInheritedMembers();
|
2015-03-07 20:07:54 +01:00
|
|
|
checkClass.checkExplicitConstructors();
|
2018-04-24 22:42:25 +02:00
|
|
|
checkClass.checkCopyCtorAndEqOperator();
|
2018-04-27 11:12:09 +02:00
|
|
|
checkClass.checkOverride();
|
2020-02-13 10:59:00 +01:00
|
|
|
checkClass.checkThisUseAfterFree();
|
2019-08-13 20:58:31 +02:00
|
|
|
checkClass.checkUnsafeClassRefMember();
|
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();
|
|
|
|
|
2015-03-07 20:07:54 +01:00
|
|
|
/** @brief %Check that constructors with single parameter are explicit,
|
|
|
|
* if they has to be.*/
|
|
|
|
void checkExplicitConstructors();
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-11-18 16:56:00 +01:00
|
|
|
void checkMemset();
|
2015-12-25 14:19:27 +01:00
|
|
|
void checkMemsetType(const Scope *start, const Token *tok, const Scope *type, bool allocation, std::set<const Scope *> parsedTypes);
|
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 */
|
2012-05-18 16:54:58 +02:00
|
|
|
void initializerListOrder();
|
|
|
|
|
2014-08-05 11:48:53 +02:00
|
|
|
/** @brief Suggest using initialization list */
|
2012-05-18 16:54:58 +02:00
|
|
|
void initializationListUsage();
|
2011-09-28 03:07:37 +02:00
|
|
|
|
2014-08-05 11:48:53 +02:00
|
|
|
/** @brief Check for initialization of a member with itself */
|
|
|
|
void checkSelfInitialization();
|
|
|
|
|
2012-09-17 17:59:35 +02:00
|
|
|
void copyconstructors();
|
|
|
|
|
2018-04-02 15:31:47 +02:00
|
|
|
/** @brief call of virtual function in constructor/destructor */
|
|
|
|
void checkVirtualFunctionCallInConstructor();
|
2013-03-30 15:09:22 +01:00
|
|
|
|
2013-08-20 06:29:19 +02:00
|
|
|
/** @brief Check duplicated inherited members */
|
|
|
|
void checkDuplInheritedMembers();
|
|
|
|
|
2018-04-24 22:42:25 +02:00
|
|
|
/** @brief Check that copy constructor and operator defined together */
|
|
|
|
void checkCopyCtorAndEqOperator();
|
2017-03-24 12:00:20 +01:00
|
|
|
|
2019-01-14 19:54:34 +01:00
|
|
|
/** @brief Check that the override keyword is used when overriding virtual functions */
|
2018-04-27 11:12:09 +02:00
|
|
|
void checkOverride();
|
|
|
|
|
2020-02-13 10:59:00 +01:00
|
|
|
/** @brief When "self pointer" is destroyed, 'this' might become invalid. */
|
|
|
|
void checkThisUseAfterFree();
|
|
|
|
|
2019-08-13 20:58:31 +02:00
|
|
|
/** @brief Unsafe class check - const reference member */
|
|
|
|
void checkUnsafeClassRefMember();
|
2019-08-13 20:40:48 +02:00
|
|
|
|
2010-08-07 12:26:42 +02:00
|
|
|
private:
|
2018-06-17 18:43:54 +02:00
|
|
|
const SymbolDatabase *mSymbolDatabase;
|
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);
|
2015-03-07 20:07:54 +01:00
|
|
|
void noExplicitConstructorError(const Token *tok, const std::string &classname, bool isStruct);
|
2012-09-17 18:10:11 +02:00
|
|
|
//void copyConstructorMallocError(const Token *cctor, const Token *alloc, const std::string& var_name);
|
2012-09-17 17:59:35 +02:00
|
|
|
void copyConstructorShallowCopyError(const Token *tok, const std::string& varname);
|
2018-05-04 15:39:23 +02:00
|
|
|
void noCopyConstructorError(const Scope *scope, bool isdefault, const Token *alloc, bool inconclusive);
|
2018-05-05 07:46:58 +02:00
|
|
|
void noOperatorEqError(const Scope *scope, bool isdefault, const Token *alloc, bool inconclusive);
|
2018-05-04 15:39:23 +02:00
|
|
|
void noDestructorError(const Scope *scope, bool isdefault, const Token *alloc);
|
2021-02-01 18:58:51 +01:00
|
|
|
void uninitVarError(const Token *tok, bool isprivate, Function::Type functionType, const std::string &classname, const std::string &varname, bool derived, bool inconclusive);
|
2012-09-20 19:16:26 +02:00
|
|
|
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive);
|
2010-01-16 08:47:46 +01:00
|
|
|
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);
|
2014-03-15 18:22:29 +01:00
|
|
|
void memsetErrorReference(const Token *tok, const std::string &memfunc, const std::string &type);
|
2014-08-20 15:12:53 +02:00
|
|
|
void memsetErrorFloat(const Token *tok, const std::string &type);
|
2013-03-01 15:07:20 +01:00
|
|
|
void mallocOnClassError(const Token* tok, const std::string &memfunc, const Token* classTok, const std::string &classname);
|
|
|
|
void mallocOnClassWarning(const Token* tok, const std::string &memfunc, const Token* classTok);
|
2014-06-14 12:55:20 +02:00
|
|
|
void virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived, bool inconclusive);
|
2010-01-16 08:47:46 +01:00
|
|
|
void thisSubtractionError(const Token *tok);
|
|
|
|
void operatorEqRetRefThisError(const Token *tok);
|
2015-01-24 11:18:33 +01:00
|
|
|
void operatorEqShouldBeLeftUnimplementedError(const Token *tok);
|
|
|
|
void operatorEqMissingReturnStatementError(const Token *tok, bool error);
|
2010-01-16 08:47:46 +01:00
|
|
|
void operatorEqToSelfError(const Token *tok);
|
2012-08-01 19:24:38 +02:00
|
|
|
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname, bool suggestStatic);
|
|
|
|
void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname, bool suggestStatic);
|
2011-09-28 03:07:37 +02:00
|
|
|
void initializerListError(const Token *tok1,const Token *tok2, const std::string & classname, const std::string &varname);
|
2012-05-19 21:51:39 +02:00
|
|
|
void suggestInitializationList(const Token *tok, const std::string& varname);
|
2014-08-06 13:35:39 +02:00
|
|
|
void selfInitializationError(const Token* tok, const std::string& varname);
|
2018-04-02 15:31:47 +02:00
|
|
|
void pureVirtualFunctionCallInConstructorError(const Function * scopeFunction, const std::list<const Token *> & tokStack, const std::string &purefuncname);
|
|
|
|
void virtualFunctionCallInConstructorError(const Function * scopeFunction, const std::list<const Token *> & tokStack, const std::string &funcname);
|
2018-08-19 14:13:58 +02:00
|
|
|
void duplInheritedMembersError(const Token* tok1, const Token* tok2, const std::string &derivedName, const std::string &baseName, const std::string &variableName, bool derivedIsStruct, bool baseIsStruct);
|
2018-04-24 22:42:25 +02:00
|
|
|
void copyCtorAndEqOperatorError(const Token *tok, const std::string &classname, bool isStruct, bool hasCopyCtor);
|
2018-04-27 11:12:09 +02:00
|
|
|
void overrideError(const Function *funcInBase, const Function *funcInDerived);
|
2020-02-13 10:59:00 +01:00
|
|
|
void thisUseAfterFree(const Token *self, const Token *free, const Token *use);
|
2019-08-13 20:58:31 +02:00
|
|
|
void unsafeClassRefMemberError(const Token *tok, const std::string &varname);
|
2020-12-21 07:14:52 +01:00
|
|
|
void checkDuplInheritedMembersRecursive(const Type* typeCurrent, const Type* typeBase);
|
2010-01-23 09:19:22 +01:00
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckClass c(nullptr, settings, errorLogger);
|
|
|
|
c.noConstructorError(nullptr, "classname", false);
|
|
|
|
c.noExplicitConstructorError(nullptr, "classname", false);
|
|
|
|
//c.copyConstructorMallocError(nullptr, 0, "var");
|
|
|
|
c.copyConstructorShallowCopyError(nullptr, "var");
|
2018-05-04 15:39:23 +02:00
|
|
|
c.noCopyConstructorError(nullptr, false, nullptr, false);
|
2018-05-05 07:46:58 +02:00
|
|
|
c.noOperatorEqError(nullptr, false, nullptr, false);
|
2018-05-04 15:39:23 +02:00
|
|
|
c.noDestructorError(nullptr, false, nullptr);
|
2021-02-01 18:58:51 +01:00
|
|
|
c.uninitVarError(nullptr, false, Function::eConstructor, "classname", "varname", false, false);
|
|
|
|
c.uninitVarError(nullptr, true, Function::eConstructor, "classname", "varnamepriv", false, false);
|
|
|
|
c.uninitVarError(nullptr, false, Function::eConstructor, "classname", "varname", true, false);
|
|
|
|
c.uninitVarError(nullptr, true, Function::eConstructor, "classname", "varnamepriv", true, false);
|
2017-03-01 10:50:50 +01:00
|
|
|
c.operatorEqVarError(nullptr, "classname", emptyString, false);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.unusedPrivateFunctionError(nullptr, "classname", "funcname");
|
|
|
|
c.memsetError(nullptr, "memfunc", "classname", "class");
|
|
|
|
c.memsetErrorReference(nullptr, "memfunc", "class");
|
|
|
|
c.memsetErrorFloat(nullptr, "class");
|
2017-08-09 20:00:26 +02:00
|
|
|
c.mallocOnClassWarning(nullptr, "malloc", nullptr);
|
|
|
|
c.mallocOnClassError(nullptr, "malloc", nullptr, "std::string");
|
2016-05-07 16:30:54 +02:00
|
|
|
c.virtualDestructorError(nullptr, "Base", "Derived", false);
|
|
|
|
c.thisSubtractionError(nullptr);
|
|
|
|
c.operatorEqRetRefThisError(nullptr);
|
|
|
|
c.operatorEqMissingReturnStatementError(nullptr, true);
|
|
|
|
c.operatorEqShouldBeLeftUnimplementedError(nullptr);
|
|
|
|
c.operatorEqToSelfError(nullptr);
|
|
|
|
c.checkConstError(nullptr, "class", "function", false);
|
|
|
|
c.checkConstError(nullptr, "class", "function", true);
|
2017-08-09 20:00:26 +02:00
|
|
|
c.initializerListError(nullptr, nullptr, "class", "variable");
|
2016-05-07 16:30:54 +02:00
|
|
|
c.suggestInitializationList(nullptr, "variable");
|
|
|
|
c.selfInitializationError(nullptr, "var");
|
2017-08-09 20:00:26 +02:00
|
|
|
c.duplInheritedMembersError(nullptr, nullptr, "class", "class", "variable", false, false);
|
2018-04-24 22:42:25 +02:00
|
|
|
c.copyCtorAndEqOperatorError(nullptr, "class", false, false);
|
2018-04-02 15:31:47 +02:00
|
|
|
c.pureVirtualFunctionCallInConstructorError(nullptr, std::list<const Token *>(), "f");
|
|
|
|
c.virtualFunctionCallInConstructorError(nullptr, std::list<const Token *>(), "f");
|
2018-04-27 11:12:09 +02:00
|
|
|
c.overrideError(nullptr, nullptr);
|
2020-02-13 10:59:00 +01:00
|
|
|
c.thisUseAfterFree(nullptr, nullptr, nullptr);
|
2019-08-13 20:58:31 +02:00
|
|
|
c.unsafeClassRefMemberError(nullptr, "UnsafeClass::var");
|
2010-01-16 08:47:46 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2010-01-16 08:47:46 +01:00
|
|
|
return "Class";
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
2010-01-16 08:47:46 +01:00
|
|
|
return "Check the code for each class.\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- Missing constructors and copy constructors\n"
|
|
|
|
//"- Missing allocation of memory in copy constructor\n"
|
2015-11-13 12:48:26 +01:00
|
|
|
"- Constructors which should be explicit\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- Are all variables initialized by the constructors?\n"
|
|
|
|
"- Are all variables assigned by 'operator='?\n"
|
|
|
|
"- Warn if memset, memcpy etc are used on a class\n"
|
|
|
|
"- Warn if memory for classes is allocated with malloc()\n"
|
|
|
|
"- If it's a base class, check that the destructor is virtual\n"
|
|
|
|
"- Are there unused private functions?\n"
|
|
|
|
"- 'operator=' should check for assignment to self\n"
|
|
|
|
"- Constness for member functions\n"
|
|
|
|
"- Order of initializations\n"
|
|
|
|
"- Suggest usage of initialization list\n"
|
|
|
|
"- Initialization of a member with itself\n"
|
|
|
|
"- Suspicious subtraction from 'this'\n"
|
|
|
|
"- Call of pure virtual function in constructor/destructor\n"
|
2017-03-24 12:00:20 +01:00
|
|
|
"- Duplicated inherited data members\n"
|
2018-09-08 09:14:02 +02:00
|
|
|
// disabled for now "- If 'copy constructor' defined, 'operator=' also should be defined and vice versa\n"
|
2018-04-27 11:12:09 +02:00
|
|
|
"- Check that arbitrary usage of public interface does not result in division by zero\n"
|
2020-02-13 10:59:00 +01:00
|
|
|
"- Delete \"self pointer\" and then access 'this'\n"
|
2020-01-09 06:53:08 +01:00
|
|
|
"- Check that the 'override' keyword is used when overriding virtual functions\n";
|
2010-01-16 08:47:46 +01:00
|
|
|
}
|
2010-09-11 08:23:30 +02:00
|
|
|
|
2014-07-24 14:54:20 +02:00
|
|
|
// operatorEqRetRefThis helper functions
|
|
|
|
void checkReturnPtrThis(const Scope *scope, const Function *func, const Token *tok, const Token *last);
|
|
|
|
void checkReturnPtrThis(const Scope *scope, const Function *func, const Token *tok, const Token *last, std::set<const Function*>& analyzedFunctions);
|
2010-11-14 06:50:33 +01:00
|
|
|
|
|
|
|
// operatorEqToSelf helper functions
|
2014-03-15 18:22:29 +01:00
|
|
|
bool hasAllocation(const Function *func, const Scope* scope) const;
|
2021-02-02 15:00:46 +01:00
|
|
|
bool hasAllocation(const Function *func, const Scope* scope, const Token *start, const Token *end) const;
|
|
|
|
bool hasAllocationInIfScope(const Function *func, const Scope* scope, const Token *ifStatementScopeStart) const;
|
|
|
|
static bool hasAssignSelf(const Function *func, const Token *rhs, const Token **out_ifStatementScopeStart);
|
|
|
|
enum class Bool { TRUE, FALSE, BAILOUT };
|
|
|
|
static Bool isInverted(const Token *tok, const Token *rhs);
|
|
|
|
static const Token * getIfStmtBodyStart(const Token *tok, const Token *rhs);
|
2010-11-14 06:50:33 +01:00
|
|
|
|
2011-01-16 11:18:12 +01:00
|
|
|
// checkConst helper functions
|
2014-03-15 18:22:29 +01:00
|
|
|
bool isMemberVar(const Scope *scope, const Token *tok) const;
|
|
|
|
bool isMemberFunc(const Scope *scope, const Token *tok) const;
|
|
|
|
bool isConstMemberFunc(const Scope *scope, const Token *tok) const;
|
2014-05-10 11:49:14 +02:00
|
|
|
bool checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) 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 {
|
2021-02-01 19:31:30 +01:00
|
|
|
explicit Usage(const Variable *var) : var(var), assign(false), init(false) { }
|
2021-02-01 17:13:58 +01:00
|
|
|
|
|
|
|
/** Variable that this usage is for */
|
|
|
|
const Variable *var;
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/** @brief has this variable been assigned? */
|
2012-03-11 11:01:39 +01:00
|
|
|
bool assign;
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/** @brief has this variable been initialized? */
|
2012-03-11 11:01:39 +01:00
|
|
|
bool init;
|
2011-01-16 16:37:11 +01:00
|
|
|
};
|
|
|
|
|
2012-05-17 10:33:24 +02:00
|
|
|
static bool isBaseClassFunc(const Token *tok, const Scope *scope);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
2021-02-01 18:58:51 +01:00
|
|
|
/**
|
|
|
|
* @brief Create usage list that contains all scope members and also members
|
|
|
|
* of base classes without constructors.
|
|
|
|
* @param scope current class scope
|
|
|
|
*/
|
2021-02-03 19:13:49 +01:00
|
|
|
static std::vector<Usage> createUsageList(const Scope *scope);
|
2021-02-01 18:58:51 +01:00
|
|
|
|
2011-01-16 16:37:11 +01:00
|
|
|
/**
|
|
|
|
* @brief assign a variable in the varlist
|
2021-02-01 18:58:51 +01:00
|
|
|
* @param usageList reference to usage vector
|
2015-11-27 19:32:28 +01:00
|
|
|
* @param varid id of variable to mark assigned
|
2011-01-16 16:37:11 +01:00
|
|
|
*/
|
2021-02-01 18:58:51 +01:00
|
|
|
static void assignVar(std::vector<Usage> &usageList, nonneg int varid);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief initialize a variable in the varlist
|
2021-02-01 17:13:58 +01:00
|
|
|
* @param usageList reference to usage vector
|
2015-11-27 19:32:28 +01:00
|
|
|
* @param varid id of variable to mark initialized
|
2011-01-16 16:37:11 +01:00
|
|
|
*/
|
2021-02-01 17:13:58 +01:00
|
|
|
static void initVar(std::vector<Usage> &usageList, nonneg int varid);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief set all variables in list assigned
|
2021-02-01 17:13:58 +01:00
|
|
|
* @param usageList reference to usage vector
|
2011-01-16 16:37:11 +01:00
|
|
|
*/
|
2021-02-01 17:13:58 +01:00
|
|
|
static void assignAllVar(std::vector<Usage> &usageList);
|
2011-01-16 16:37:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief set all variables in list not assigned and not initialized
|
2021-02-01 17:13:58 +01:00
|
|
|
* @param usageList reference to usage vector
|
2011-01-16 16:37:11 +01:00
|
|
|
*/
|
2021-02-01 17:13:58 +01:00
|
|
|
static void clearAllVar(std::vector<Usage> &usageList);
|
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
|
|
|
|
*/
|
2013-01-01 09:53:40 +01:00
|
|
|
void initializeVarList(const Function &func, std::list<const Function *> &callstack, const Scope *scope, std::vector<Usage> &usage);
|
2011-01-18 07:34:11 +01:00
|
|
|
|
2013-03-30 15:09:22 +01:00
|
|
|
/**
|
2019-07-04 12:32:32 +02:00
|
|
|
* @brief gives a list of tokens where virtual functions are called directly or indirectly
|
2013-03-30 15:09:22 +01:00
|
|
|
* @param function function to be checked
|
2018-04-02 15:31:47 +02:00
|
|
|
* @param virtualFunctionCallsMap map of results for already checked functions
|
2013-03-30 15:09:22 +01:00
|
|
|
* @return list of tokens where pure virtual functions are called
|
|
|
|
*/
|
2018-04-02 15:31:47 +02:00
|
|
|
const std::list<const Token *> & getVirtualFunctionCalls(
|
2013-03-30 15:09:22 +01:00
|
|
|
const Function & function,
|
2018-04-02 15:31:47 +02:00
|
|
|
std::map<const Function *, std::list<const Token *> > & virtualFunctionCallsMap);
|
2013-03-30 15:09:22 +01:00
|
|
|
|
|
|
|
/**
|
2019-07-04 12:32:32 +02:00
|
|
|
* @brief looks for the first virtual function call stack
|
2018-04-02 15:31:47 +02:00
|
|
|
* @param virtualFunctionCallsMap map of results obtained from getVirtualFunctionCalls
|
|
|
|
* @param callToken token where pure virtual function is called directly or indirectly
|
2013-03-30 15:09:22 +01:00
|
|
|
* @param[in,out] pureFuncStack list to append the stack
|
|
|
|
*/
|
2018-04-02 15:31:47 +02:00
|
|
|
void getFirstVirtualFunctionCallStack(
|
|
|
|
std::map<const Function *, std::list<const Token *> > & virtualFunctionCallsMap,
|
|
|
|
const Token *callToken,
|
2013-03-30 15:09:22 +01:00
|
|
|
std::list<const Token *> & pureFuncStack);
|
|
|
|
|
2012-08-02 18:50:48 +02:00
|
|
|
static bool canNotCopy(const Scope *scope);
|
2013-04-10 21:57:22 +02:00
|
|
|
|
|
|
|
static bool canNotMove(const Scope *scope);
|
2020-02-13 10:59:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper for checkThisUseAfterFree
|
|
|
|
*/
|
|
|
|
bool checkThisUseAfterFreeRecursive(const Scope *classScope, const Function *func, const Variable *selfPointer, std::set<const Function *> callstack, const Token **freeToken);
|
|
|
|
|
2010-01-16 08:47:46 +01:00
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkclassH
|