53 friend class TestClass;
54 friend class TestConstructors;
55 friend class TestUnusedPrivateFunction;
104 void checkExplicitConstructors();
107 void privateFunctions();
118 void checkMemsetType(
const Scope *start,
const Token *tok,
const Scope *type,
bool allocation, std::set<const Scope *> parsedTypes);
121 void operatorEqRetRefThis();
124 void operatorEqToSelf();
127 void virtualDestructor();
130 void thisSubtraction();
136 void initializerListOrder();
139 void initializationListUsage();
142 void checkSelfInitialization();
144 void copyconstructors();
147 void checkVirtualFunctionCallInConstructor();
150 void checkDuplInheritedMembers();
153 void checkCopyCtorAndEqOperator();
156 void checkOverride();
159 void checkUselessOverride();
162 void checkReturnByReference();
165 void checkThisUseAfterFree();
168 void checkUnsafeClassRefMember();
181 void noConstructorError(
const Token *tok,
const std::string &classname,
bool isStruct);
182 void noExplicitConstructorError(
const Token *tok,
const std::string &classname,
bool isStruct);
184 void copyConstructorShallowCopyError(
const Token *tok,
const std::string& varname);
187 void noDestructorError(
const Scope *scope,
bool isdefault,
const Token *alloc);
188 void uninitVarError(
const Token *tok,
bool isprivate,
Function::Type functionType,
const std::string &classname,
const std::string &varname,
bool derived,
bool inconclusive);
189 void uninitVarError(
const Token *tok,
const std::string &classname,
const std::string &varname);
190 void missingMemberCopyError(
const Token *tok,
Function::Type functionType,
const std::string& classname,
const std::string& varname);
191 void operatorEqVarError(
const Token *tok,
const std::string &classname,
const std::string &varname,
bool inconclusive);
192 void unusedPrivateFunctionError(
const Token *tok,
const std::string &classname,
const std::string &funcname);
193 void memsetError(
const Token *tok,
const std::string &memfunc,
const std::string &classname,
const std::string &type,
bool isContainer =
false);
194 void memsetErrorReference(
const Token *tok,
const std::string &memfunc,
const std::string &type);
195 void memsetErrorFloat(
const Token *tok,
const std::string &type);
196 void mallocOnClassError(
const Token* tok,
const std::string &memfunc,
const Token* classTok,
const std::string &classname);
197 void mallocOnClassWarning(
const Token* tok,
const std::string &memfunc,
const Token* classTok);
198 void virtualDestructorError(
const Token *tok,
const std::string &Base,
const std::string &Derived,
bool inconclusive);
199 void thisSubtractionError(
const Token *tok);
200 void operatorEqRetRefThisError(
const Token *tok);
201 void operatorEqShouldBeLeftUnimplementedError(
const Token *tok);
202 void operatorEqMissingReturnStatementError(
const Token *tok,
bool error);
203 void operatorEqToSelfError(
const Token *tok);
204 void checkConstError(
const Token *tok,
const std::string &classname,
const std::string &funcname,
bool suggestStatic);
205 void checkConstError2(
const Token *tok1,
const Token *tok2,
const std::string &classname,
const std::string &funcname,
bool suggestStatic);
206 void initializerListError(
const Token *tok1,
const Token *tok2,
const std::string & classname,
const std::string &varname,
const std::string& argname = {});
207 void suggestInitializationList(
const Token *tok,
const std::string& varname);
208 void selfInitializationError(
const Token* tok,
const std::string& varname);
209 void pureVirtualFunctionCallInConstructorError(
const Function * scopeFunction,
const std::list<const Token *> & tokStack,
const std::string &purefuncname);
210 void virtualFunctionCallInConstructorError(
const Function * scopeFunction,
const std::list<const Token *> & tokStack,
const std::string &funcname);
211 void duplInheritedMembersError(
const Token* tok1,
const Token* tok2,
const std::string &derivedName,
const std::string &baseName,
const std::string &memberName,
bool derivedIsStruct,
bool baseIsStruct,
bool isFunction =
false);
212 void copyCtorAndEqOperatorError(
const Token *tok,
const std::string &classname,
bool isStruct,
bool hasCopyCtor);
213 void overrideError(
const Function *funcInBase,
const Function *funcInDerived);
214 void uselessOverrideError(
const Function *funcInBase,
const Function *funcInDerived,
bool isSameCode =
false);
216 void thisUseAfterFree(
const Token *
self,
const Token *free,
const Token *use);
217 void unsafeClassRefMemberError(
const Token *tok,
const std::string &varname);
218 void checkDuplInheritedMembersRecursive(
const Type* typeCurrent,
const Type* typeBase);
236 c.
memsetError(
nullptr,
"memfunc",
"classname",
"class");
268 return "Check the code for each class.\n"
269 "- Missing constructors and copy constructors\n"
271 "- Constructors which should be explicit\n"
272 "- Are all variables initialized by the constructors?\n"
273 "- Are all variables assigned by 'operator='?\n"
274 "- Warn if memset, memcpy etc are used on a class\n"
275 "- Warn if memory for classes is allocated with malloc()\n"
276 "- If it's a base class, check that the destructor is virtual\n"
277 "- Are there unused private functions?\n"
278 "- 'operator=' should check for assignment to self\n"
279 "- Constness for member functions\n"
280 "- Order of initializations\n"
281 "- Suggest usage of initialization list\n"
282 "- Initialization of a member with itself\n"
283 "- Suspicious subtraction from 'this'\n"
284 "- Call of pure virtual function in constructor/destructor\n"
285 "- Duplicated inherited data members\n"
287 "- Check that arbitrary usage of public interface does not result in division by zero\n"
288 "- Delete \"self pointer\" and then access 'this'\n"
289 "- Check that the 'override' keyword is used when overriding virtual functions\n"
290 "- Check that the 'one definition rule' is not violated\n";
295 void checkReturnPtrThis(
const Scope *scope,
const Function *func,
const Token *tok,
const Token *last, std::set<const Function*>& analyzedFunctions);
298 bool hasAllocation(
const Function *func,
const Scope* scope)
const;
300 bool hasAllocationInIfScope(
const Function *func,
const Scope* scope,
const Token *ifStatementScopeStart)
const;
301 static bool hasAssignSelf(
const Function *func,
const Token *rhs,
const Token *&out_ifStatementScopeStart);
302 enum class Bool { TRUE, FALSE, BAILOUT };
303 static Bool isInverted(
const Token *tok,
const Token *rhs);
304 static const Token * getIfStmtBodyStart(
const Token *tok,
const Token *rhs);
307 bool isMemberVar(
const Scope *scope,
const Token *tok)
const;
308 static bool isMemberFunc(
const Scope *scope,
const Token *tok);
309 static bool isConstMemberFunc(
const Scope *scope,
const Token *tok);
311 bool checkConstFunc(
const Scope *scope,
const Function *func, MemberAccess& memberAccessed)
const;
328 static bool isBaseClassMutableMemberFunc(
const Token *tok,
const Scope *scope);
335 static std::vector<Usage> createUsageList(
const Scope *scope);
342 static void assignVar(std::vector<Usage> &usageList,
nonneg int varid);
349 static void assignVar(std::vector<Usage> &usageList,
const Token *vartok);
356 static void initVar(std::vector<Usage> &usageList,
nonneg int varid);
362 static void assignAllVar(std::vector<Usage> &usageList);
369 static void assignAllVarsVisibleFromScope(std::vector<Usage> &usageList,
const Scope *scope);
375 static void clearAllVar(std::vector<Usage> &usageList);
384 void initializeVarList(
const Function &func, std::list<const Function *> &callstack,
const Scope *scope, std::vector<Usage> &usage)
const;
392 const std::list<const Token *> & getVirtualFunctionCalls(
394 std::map<
const Function *, std::list<const Token *>> & virtualFunctionCallsMap);
402 static void getFirstVirtualFunctionCallStack(
403 std::map<
const Function *, std::list<const Token *>> & virtualFunctionCallsMap,
404 const Token *callToken,
405 std::list<const Token *> & pureFuncStack);
407 static bool canNotCopy(
const Scope *scope);
409 static bool canNotMove(
const Scope *scope);
414 bool checkThisUseAfterFreeRecursive(
const Scope *classScope,
const Function *func,
const Variable *selfPointer, std::set<const Function *> callstack,
const Token *&freeToken);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void operatorEqMissingReturnStatementError(const Token *tok, bool error)
void overrideError(const Function *funcInBase, const Function *funcInDerived)
void checkCopyCtorAndEqOperator()
Check that copy constructor and operator defined together.
void checkOverride()
Check that the override keyword is used when overriding virtual functions.
void uninitVarError(const Token *tok, bool isprivate, Function::Type functionType, const std::string &classname, const std::string &varname, bool derived, bool inconclusive)
void thisSubtraction()
warn for "this-x".
void checkThisUseAfterFree()
When "self pointer" is destroyed, 'this' might become invalid.
void initializerListError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &varname, const std::string &argname={})
void noExplicitConstructorError(const Token *tok, const std::string &classname, bool isStruct)
void operatorEqRetRefThis()
'operator=' should return reference to *this
void operatorEqToSelfError(const Token *tok)
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)
void duplInheritedMembersError(const Token *tok1, const Token *tok2, const std::string &derivedName, const std::string &baseName, const std::string &memberName, bool derivedIsStruct, bool baseIsStruct, bool isFunction=false)
void uselessOverrideError(const Function *funcInBase, const Function *funcInDerived, bool isSameCode=false)
void mallocOnClassWarning(const Token *tok, const std::string &memfunc, const Token *classTok)
void copyConstructorShallowCopyError(const Token *tok, const std::string &varname)
void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname)
void checkUnsafeClassRefMember()
Unsafe class check - const reference member.
void virtualFunctionCallInConstructorError(const Function *scopeFunction, const std::list< const Token * > &tokStack, const std::string &funcname)
CheckClass()
This constructor is used when registering the CheckClass.
void thisSubtractionError(const Token *tok)
void checkReturnByReference()
Check that large members are returned by reference from getter function.
void checkDuplInheritedMembers()
Check duplicated inherited members.
void operatorEqToSelf()
'operator=' should check for assignment to self
void selfInitializationError(const Token *tok, const std::string &varname)
void operatorEqShouldBeLeftUnimplementedError(const Token *tok)
void mallocOnClassError(const Token *tok, const std::string &memfunc, const Token *classTok, const std::string &classname)
void noConstructorError(const Token *tok, const std::string &classname, bool isStruct)
void privateFunctions()
Check that all private functions are called
void virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived, bool inconclusive)
void checkUselessOverride()
Check that the overriden function is not identical to the base function.
void initializerListOrder()
Check initializer list order.
void initializationListUsage()
Suggest using initialization list.
void checkMemset()
Check that the memsets are valid.
void noDestructorError(const Scope *scope, bool isdefault, const Token *alloc)
void missingMemberCopyError(const Token *tok, Function::Type functionType, const std::string &classname, const std::string &varname)
static std::string myName()
void checkVirtualFunctionCallInConstructor()
call of virtual function in constructor/destructor
std::string classInfo() const override
get information about this class, used to generate documentation
void memsetErrorFloat(const Token *tok, const std::string &type)
void noCopyConstructorError(const Scope *scope, bool isdefault, const Token *alloc, bool inconclusive)
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname, bool suggestStatic)
void constructors()
Check that all class constructors are ok
void checkSelfInitialization()
Check for initialization of a member with itself.
void operatorEqRetRefThisError(const Token *tok)
void noOperatorEqError(const Scope *scope, bool isdefault, const Token *alloc, bool inconclusive)
static const std::set< std::string > stl_containers_not_const
Set of the STL types whose operator[] is not const.
void virtualDestructor()
The destructor in a base class should be virtual.
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks on the normal token list.
void suggestInitializationList(const Token *tok, const std::string &varname)
void returnByReferenceError(const Function *func, const Variable *var)
void memsetErrorReference(const Token *tok, const std::string &memfunc, const std::string &type)
void pureVirtualFunctionCallInConstructorError(const Function *scopeFunction, const std::list< const Token * > &tokStack, const std::string &purefuncname)
void unsafeClassRefMemberError(const Token *tok, const std::string &varname)
void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type, bool isContainer=false)
void checkConst()
can member function be const?
void thisUseAfterFree(const Token *self, const Token *free, const Token *use)
void checkExplicitConstructors()
Check that constructors with single parameter are explicit, if they has to be.
void copyCtorAndEqOperatorError(const Token *tok, const std::string &classname, bool isStruct, bool hasCopyCtor)
Base class used for whole-program analysis.
Interface class that cppcheck uses to communicate with the checks.
virtual bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list< FileInfo * > &fileInfo, const Settings &, ErrorLogger &)
virtual FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const
virtual FileInfo * getFileInfo(const Tokenizer &, const Settings &) const
This is an interface, which the class responsible of error logging should implement.
This is just a container for general settings so that we don't need to pass individual values to func...
The token list that the TokenList generates is a linked-list of this class.
The main purpose is to tokenize the source code.
const Settings & getSettings() const
bool isC() const
Is the code C.
Information about a class type.
Information about a member variable.
static const std::string emptyString
@ error
Programming error.
Whole program analysis (ctu=Cross Translation Unit)
Information about a member variable.
const Variable * var
Variable that this usage is for.
Usage(const Variable *var)