cleaned up access of the check classes (#5387)

This commit is contained in:
Oliver Stöneberg 2023-09-11 11:12:42 +02:00 committed by GitHub
parent 64cd09ab62
commit bbe45ff0eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 145 additions and 84 deletions

View File

@ -61,10 +61,12 @@ public:
/** This constructor is used when registering the CheckClass */
explicit Check(const std::string &aname);
protected:
/** This constructor is used when running checks. */
Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger), mName(std::move(aname)) {}
public:
virtual ~Check() {
if (!mTokenizer)
instances().remove(this);
@ -127,9 +129,9 @@ public:
return false;
}
protected:
static std::string getMessageId(const ValueFlow::Value &value, const char id[]);
protected:
const Tokenizer* const mTokenizer{};
const Settings* const mSettings{};
ErrorLogger* const mErrorLogger{};

View File

@ -41,10 +41,13 @@ class Token;
*/
class CPPCHECKLIB Check64BitPortability : public Check {
friend class Test64BitPortability;
public:
/** This constructor is used when registering the Check64BitPortability */
Check64BitPortability() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
Check64BitPortability(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -58,8 +61,6 @@ public:
/** Check for pointer assignment */
void pointerassignment();
private:
void assignmentAddressToIntegerError(const Token *tok);
void assignmentIntegerToAddressError(const Token *tok);
void returnIntegerError(const Token *tok);

View File

@ -41,9 +41,12 @@ class Token;
*/
class CPPCHECKLIB CheckAssert : public Check {
friend class TestFixture;
public:
CheckAssert() : Check(myName()) {}
private:
CheckAssert(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -55,11 +58,9 @@ public:
void assertWithSideEffects();
protected:
void checkVariableAssignment(const Token* assignTok, const Scope *assertionScope);
static bool inSameScope(const Token* returnTok, const Token* assignTok);
private:
void sideEffectInAssertError(const Token *tok, const std::string& functionName);
void assignmentInAssertError(const Token *tok, const std::string &varname);

View File

@ -45,10 +45,13 @@ namespace ValueFlow {
class CPPCHECKLIB CheckAutoVariables : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckClass */
CheckAutoVariables() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckAutoVariables(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -76,7 +79,6 @@ public:
void checkVarLifetimeScope(const Token * start, const Token * end);
private:
void errorAutoVariableAssignment(const Token *tok, bool inconclusive);
void errorReturnDanglingLifetime(const Token *tok, const ValueFlow::Value* val);
void errorInvalidLifetime(const Token *tok, const ValueFlow::Value* val);

View File

@ -39,10 +39,13 @@ class Token;
/** @brief checks dealing with suspicious usage of boolean type (not for evaluating conditions) */
class CPPCHECKLIB CheckBool : public Check {
friend class TestFixture;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckBool() : Check(myName()) {}
private:
/** @brief This constructor is used when running checks. */
CheckBool(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -95,7 +98,6 @@ public:
/** @brief %Check if a function returning bool returns an integer other than 0 or 1 */
void returnValueOfFunctionReturningBool();
private:
// Error messages..
void comparisonOfFuncReturningBoolError(const Token *tok, const std::string &expression);
void comparisonOfTwoFuncsReturningBoolError(const Token *tok, const std::string &expression1, const std::string &expression2);

View File

@ -38,10 +38,13 @@ class Token;
/** @brief %Check Boost usage */
class CPPCHECKLIB CheckBoost : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckClass */
CheckBoost() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckBoost(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -58,7 +61,6 @@ public:
/** @brief %Check for container modification while using the BOOST_FOREACH macro */
void checkBoostForeachModification();
private:
void boostForeachError(const Token *tok);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {

View File

@ -57,11 +57,14 @@ class Token;
* other function and pass a buffer and reads or writes too much data.
*/
class CPPCHECKLIB CheckBufferOverrun : public Check {
public:
friend class TestBufferOverrun;
friend class TestFixture;
public:
/** This constructor is used when registering the CheckClass */
CheckBufferOverrun() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckBufferOverrun(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -97,8 +100,6 @@ public:
/** @brief Analyse all file infos for all TU */
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
private:
void arrayIndex();
void arrayIndexError(const Token* tok,
const std::vector<Dimension>& dimensions,

View File

@ -51,10 +51,18 @@ namespace tinyxml2 {
/** @brief %Check classes. Uninitialized member variables, non-conforming operators, missing virtual destructor, etc */
class CPPCHECKLIB CheckClass : public Check {
friend class TestClass;
friend class TestConstructors;
friend class TestUnusedPrivateFunction;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckClass() : Check(myName()) {}
/** @brief Set of the STL types whose operator[] is not const */
static const std::set<std::string> stl_containers_not_const;
private:
/** @brief This constructor is used when running checks. */
CheckClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger);
@ -191,10 +199,6 @@ public:
/** @brief Analyse all file infos for all TU */
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
/** @brief Set of the STL types whose operator[] is not const */
static const std::set<std::string> stl_containers_not_const;
private:
const SymbolDatabase* mSymbolDatabase{};
// Reporting errors..

View File

@ -48,10 +48,13 @@ namespace ValueFlow {
*/
class CPPCHECKLIB CheckCondition : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckAssignIf */
CheckCondition() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckCondition(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -128,7 +131,6 @@ public:
/** @brief Assignment in condition */
void checkAssignmentInCondition();
private:
// The conditions that have been diagnosed
std::set<const Token*> mCondDiags;
bool diag(const Token* tok, bool insert=true);

View File

@ -45,10 +45,13 @@ class Token;
*/
class CPPCHECKLIB CheckExceptionSafety : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckClass */
CheckExceptionSafety() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckExceptionSafety(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -88,7 +91,6 @@ public:
/** @brief %Check for rethrow not from catch scope */
void rethrowNoCurrentException();
private:
/** Don't throw exceptions in destructors */
void destructorsError(const Token * const tok, const std::string &className);
void deallocThrowError(const Token * const tok, const std::string &varname);

View File

@ -49,10 +49,14 @@ namespace ValueFlow {
*/
class CPPCHECKLIB CheckFunctions : public Check {
friend class TestFunctions;
friend class TestFixture;
public:
/** This constructor is used when registering the CheckFunctions */
CheckFunctions() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -109,7 +113,6 @@ public:
/** @brief --check-library: warn for unconfigured function calls */
void checkLibraryMatchFunctions();
private:
/** @brief %Check for missing "return" */
void checkMissingReturn();

View File

@ -39,10 +39,12 @@ class Token;
/** @brief %Check Internal cppcheck API usage */
class CPPCHECKLIB CheckInternal : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckClass */
CheckInternal() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckInternal(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -82,7 +84,7 @@ public:
/** @brief %Check if there is a redundant check for none-nullness of parameter before Match functions, such as (tok && Token::Match(tok, "foo")) */
void checkRedundantTokCheck();
private:
void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
void complexPatternError(const Token *tok, const std::string &pattern, const std::string &funcname);

View File

@ -40,10 +40,13 @@ class ErrorLogger;
/** @brief %Check input output operations. */
class CPPCHECKLIB CheckIO : public Check {
friend class TestIO;
public:
/** @brief This constructor is used when registering CheckIO */
CheckIO() : Check(myName()) {}
private:
/** @brief This constructor is used when running checks. */
CheckIO(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -70,7 +73,6 @@ public:
/** @brief %Checks type and number of arguments given to functions like printf or scanf*/
void checkWrongPrintfScanfArguments();
private:
class ArgumentInfo {
public:
ArgumentInfo(const Token *arg, const Settings *settings, bool _isCPP);

View File

@ -107,10 +107,13 @@ public:
*/
class CPPCHECKLIB CheckLeakAutoVar : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckLeakAutoVar */
CheckLeakAutoVar() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckLeakAutoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -120,8 +123,6 @@ public:
checkLeakAutoVar.check();
}
private:
/** check for leaks in all scopes */
void check();

View File

@ -163,11 +163,14 @@ public:
* -# finally, check if the simplified token list contain any leaks.
*/
class CPPCHECKLIB CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak {
class CPPCHECKLIB CheckMemoryLeakInFunction : public Check, public CheckMemoryLeak {
friend class TestMemleakInFunction;
public:
/** @brief This constructor is used when registering this class */
CheckMemoryLeakInFunction() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {}
private:
/** @brief This constructor is used when running checks */
CheckMemoryLeakInFunction(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {}
@ -182,7 +185,6 @@ public:
*/
void checkReallocUsage();
private:
/** Report all possible errors (for the --errorlist) */
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override {
CheckMemoryLeakInFunction c(nullptr, settings, e);
@ -217,10 +219,13 @@ private:
* @brief %Check class variables, variables that are allocated in the constructor should be deallocated in the destructor
*/
class CPPCHECKLIB CheckMemoryLeakInClass : private Check, private CheckMemoryLeak {
class CPPCHECKLIB CheckMemoryLeakInClass : public Check, private CheckMemoryLeak {
friend class TestMemleakInClass;
public:
CheckMemoryLeakInClass() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {}
private:
CheckMemoryLeakInClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {}
@ -234,7 +239,6 @@ public:
void check();
private:
void variable(const Scope *scope, const Token *tokVarname);
/** Public functions: possible double-allocation */
@ -262,10 +266,13 @@ private:
/** @brief detect simple memory leaks for struct members */
class CPPCHECKLIB CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak {
class CPPCHECKLIB CheckMemoryLeakStructMember : public Check, private CheckMemoryLeak {
friend class TestMemleakStructMember;
public:
CheckMemoryLeakStructMember() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {}
private:
CheckMemoryLeakStructMember(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {}
@ -276,8 +283,6 @@ public:
void check();
private:
/** Is local variable allocated with malloc? */
bool isMalloc(const Variable *variable) const;
@ -298,10 +303,13 @@ private:
/** @brief detect simple memory leaks (address not taken) */
class CPPCHECKLIB CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak {
class CPPCHECKLIB CheckMemoryLeakNoVar : public Check, private CheckMemoryLeak {
friend class TestMemleakNoVar;
public:
CheckMemoryLeakNoVar() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {}
private:
CheckMemoryLeakNoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {}
@ -312,7 +320,6 @@ public:
void check();
private:
/**
* @brief %Check if an input argument to a function is the return value of an allocation function
* like malloc(), and the function does not release it.

View File

@ -47,32 +47,13 @@ namespace tinyxml2 {
/** @brief check for null pointer dereferencing */
class CPPCHECKLIB CheckNullPointer : public Check {
friend class TestNullPointer;
friend class TestFixture;
public:
/** @brief This constructor is used when registering the CheckNullPointer */
CheckNullPointer() : Check(myName()) {}
/** @brief This constructor is used when running checks. */
CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
CheckNullPointer checkNullPointer(&tokenizer, tokenizer.getSettings(), errorLogger);
checkNullPointer.nullPointer();
checkNullPointer.arithmetic();
checkNullPointer.nullConstantDereference();
}
/**
* @brief parse a function call and extract information about variable usage
* @param tok first token
* @param var variables that the function read / write.
* @param library --library files data
*/
static void parseFunctionCall(const Token &tok,
std::list<const Token *> &var,
const Library *library);
/**
* Is there a pointer dereference? Everything that should result in
* a nullpointer dereference error message will result in a true
@ -86,6 +67,29 @@ public:
static bool isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings);
private:
/**
* @brief parse a function call and extract information about variable usage
* @param tok first token
* @param var variables that the function read / write.
* @param library --library files data
*/
static void parseFunctionCall(const Token &tok,
std::list<const Token *> &var,
const Library *library);
/** @brief This constructor is used when running checks. */
CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
CheckNullPointer checkNullPointer(&tokenizer, tokenizer.getSettings(), errorLogger);
checkNullPointer.nullPointer();
checkNullPointer.arithmetic();
checkNullPointer.nullConstantDereference();
}
/** @brief possible null pointer dereference */
void nullPointer();
@ -117,7 +121,6 @@ public:
/** @brief Analyse all file infos for all TU */
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
private:
/** Get error messages. Used by --errorlist */
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
CheckNullPointer c(nullptr, settings, errorLogger);

View File

@ -47,14 +47,27 @@ class ErrorLogger;
/** @brief Various small checks */
class CPPCHECKLIB CheckOther : public Check {
friend class TestCharVar;
friend class TestIncompleteStatement;
friend class TestOther;
friend class TestFixture;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckOther() : Check(myName()) {}
/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is less than zero? */
static bool comparisonNonZeroExpressionLessThanZero(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr);
/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is positive? */
static bool testIfNonZeroExpressionIsPositive(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr);
private:
/** @brief This constructor is used when running checks. */
CheckOther(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
CheckOther checkOther(&tokenizer, tokenizer.getSettings(), errorLogger);
@ -103,13 +116,6 @@ public:
checkOther.checkOverlappingWrite();
}
/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is less than zero? */
static bool comparisonNonZeroExpressionLessThanZero(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr);
/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is positive? */
static bool testIfNonZeroExpressionIsPositive(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr);
/** @brief Clarify calculation for ".. a * b ? .." */
void clarifyCalculation();
@ -229,7 +235,6 @@ public:
void overlappingWriteUnion(const Token *tok);
void overlappingWriteFunction(const Token *tok);
private:
// Error messages..
void checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, const bool result);
void checkCastIntToCharAndBackError(const Token *tok, const std::string &strFunctionName);

View File

@ -40,10 +40,13 @@ class Token;
*/
class CPPCHECKLIB CheckPostfixOperator : public Check {
friend class TestPostfixOperator;
public:
/** This constructor is used when registering the CheckPostfixOperator */
CheckPostfixOperator() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckPostfixOperator(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -59,7 +62,6 @@ public:
/** Check postfix operators */
void postfixOperator();
private:
/** Report Error */
void postfixOperatorError(const Token *tok);

View File

@ -39,10 +39,13 @@ class Token;
/** @brief checks on usage of sizeof() operator */
class CPPCHECKLIB CheckSizeof : public Check {
friend class TestFixture;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckSizeof() : Check(myName()) {}
private:
/** @brief This constructor is used when running checks. */
CheckSizeof(const Tokenizer* tokenizer, const Settings* settings, ErrorLogger* errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -86,7 +89,6 @@ public:
/** @brief %Check for using sizeof(void) */
void sizeofVoid();
private:
// Error messages..
void sizeofsizeofError(const Token* tok);
void sizeofCalculationError(const Token* tok, bool inconclusive);

View File

@ -43,10 +43,13 @@ class ErrorLogger;
/** @brief %Check STL usage (invalidation of iterators, mismatching containers, etc) */
class CPPCHECKLIB CheckStl : public Check {
friend class TestFixture;
public:
/** This constructor is used when registering the CheckClass */
CheckStl() : Check(myName()) {}
private:
/** This constructor is used when running checks. */
CheckStl(const Tokenizer* tokenizer, const Settings* settings, ErrorLogger* errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -184,7 +187,6 @@ public:
void checkMutexes();
private:
bool isContainerSize(const Token *containerToken, const Token *expr) const;
bool isContainerSizeGE(const Token * containerToken, const Token *expr) const;

View File

@ -39,10 +39,13 @@ class Token;
/** @brief Detect misusage of C-style strings and related standard functions */
class CPPCHECKLIB CheckString : public Check {
friend class TestFixture;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckString() : Check(myName()) {}
private:
/** @brief This constructor is used when running checks. */
CheckString(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -82,7 +85,6 @@ public:
/** @brief %Check for overlapping source and destination passed to sprintf() */
void sprintfOverlappingData();
private:
void stringLiteralWriteError(const Token *tok, const Token *strValue);
void sprintfOverlappingDataError(const Token *funcTok, const Token *tok, const std::string &varname);
void strPlusCharError(const Token *tok);

View File

@ -42,10 +42,13 @@ class ValueType;
/** @brief Various small checks */
class CPPCHECKLIB CheckType : public Check {
friend class TestFixture;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckType() : Check(myName()) {}
private:
/** @brief This constructor is used when running checks. */
CheckType(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -77,8 +80,6 @@ public:
void checkFloatToIntegerOverflow();
void checkFloatToIntegerOverflow(const Token *tok, const ValueType *vtint, const ValueType *vtfloat, const std::list<ValueFlow::Value> &floatValues);
private:
// Error messages..
void tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits);
void tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits);

View File

@ -60,10 +60,18 @@ struct VariableValue {
/** @brief Checking for uninitialized variables */
class CPPCHECKLIB CheckUninitVar : public Check {
friend class TestUninitVar;
public:
/** @brief This constructor is used when registering the CheckUninitVar */
CheckUninitVar() : Check(myName()) {}
enum Alloc { NO_ALLOC, NO_CTOR_CALL, CTOR_CALL, ARRAY };
static const Token *isVariableUsage(bool cpp, const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
const Token *isVariableUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
private:
/** @brief This constructor is used when running checks. */
CheckUninitVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -80,15 +88,12 @@ public:
void check();
void checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs);
void checkStruct(const Token *tok, const Variable &structvar);
enum Alloc { NO_ALLOC, NO_CTOR_CALL, CTOR_CALL, ARRAY };
bool checkScopeForVariable(const Token *tok, const Variable& var, bool* const possibleInit, bool* const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue> variableValue);
const Token* checkExpr(const Token* tok, const Variable& var, const Alloc alloc, bool known, bool* bailout = nullptr) const;
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const Alloc alloc, const std::string &membervar, const bool suppressErrors);
const Token* checkLoopBodyRecursive(const Token *start, const Variable& var, const Alloc alloc, const std::string &membervar, bool &bailout) const;
void checkRhs(const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar);
static const Token *isVariableUsage(bool cpp, const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
const Token *isVariableUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
static int isFunctionParUsage(const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
int isFunctionParUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const;
@ -130,7 +135,6 @@ public:
}
void uninitStructMemberError(const Token *tok, const std::string &membername);
private:
std::set<const Token*> mUninitDiags;
Check::FileInfo* getFileInfo() const;

View File

@ -49,10 +49,10 @@ namespace CTU {
//---------------------------------------------------------------------------
// Register this check class
CheckUnusedFunctions CheckUnusedFunctions::instance;
namespace {
CheckUnusedFunctions instance;
}
static const struct CWE CWE561(561U); // Dead Code
@ -68,6 +68,12 @@ static std::string stripTemplateParameters(const std::string& funcName) {
// FUNCTION USAGE - Check for unused functions etc
//---------------------------------------------------------------------------
void CheckUnusedFunctions::clear()
{
instance.mFunctions.clear();
instance.mFunctionCalls.clear();
}
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings)
{
const bool doMarkup = settings->library.markupFile(FileName);

View File

@ -44,6 +44,8 @@ namespace CTU {
/// @{
class CPPCHECKLIB CheckUnusedFunctions : public Check {
friend class TestUnusedFunctions;
public:
/** @brief This constructor is used when registering the CheckUnusedFunctions */
CheckUnusedFunctions() : Check(myName()) {}
@ -52,10 +54,7 @@ public:
CheckUnusedFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
static void clear() {
instance.mFunctions.clear();
instance.mFunctionCalls.clear();
}
static void clear();
// Parse current tokens and determine..
// * Check what functions are used
@ -71,15 +70,12 @@ public:
/** @brief Analyse all file infos for all TU */
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
static CheckUnusedFunctions instance;
std::string analyzerInfo() const;
/** @brief Combine and analyze all analyzerInfos for all TUs */
static void analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir);
private:
void getErrorMessages(ErrorLogger *errorLogger, const Settings * /*settings*/) const override {
CheckUnusedFunctions::unusedFunctionError(errorLogger, emptyString, 0, "funcName");
}

View File

@ -45,10 +45,13 @@ class Function;
/** @brief Various small checks */
class CPPCHECKLIB CheckUnusedVar : public Check {
friend class TestUnusedVar;
public:
/** @brief This constructor is used when registering the CheckClass */
CheckUnusedVar() : Check(myName()) {}
private:
/** @brief This constructor is used when running checks. */
CheckUnusedVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -69,7 +72,6 @@ public:
/** @brief %Check that all struct members are used */
void checkStructMemberUsage();
private:
bool isRecordTypeWithoutSideEffects(const Type* type);
bool isVariableWithoutSideEffects(const Variable& var);
bool isEmptyType(const Type* type);

View File

@ -40,9 +40,12 @@ class Token;
*/
class CPPCHECKLIB CheckVaarg : public Check {
friend class TestFixture;
public:
CheckVaarg() : Check(myName()) {}
private:
CheckVaarg(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger) {}
@ -55,7 +58,6 @@ public:
void va_start_argument();
void va_list_usage();
private:
void wrongParameterTo_va_start_error(const Token *tok, const std::string& paramIsName, const std::string& paramShouldName);
void referenceAs_va_start_error(const Token *tok, const std::string& paramName);
void va_end_missingError(const Token *tok, const std::string& varname);