diff --git a/lib/check.h b/lib/check.h index 1a6c57faa..e98d1c7e3 100644 --- a/lib/check.h +++ b/lib/check.h @@ -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{}; diff --git a/lib/check64bit.h b/lib/check64bit.h index 122fd8cb0..7ce317890 100644 --- a/lib/check64bit.h +++ b/lib/check64bit.h @@ -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); diff --git a/lib/checkassert.h b/lib/checkassert.h index 7697f650d..4ddb8e156 100644 --- a/lib/checkassert.h +++ b/lib/checkassert.h @@ -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); diff --git a/lib/checkautovariables.h b/lib/checkautovariables.h index b02a0d0a6..fa4b1d92f 100644 --- a/lib/checkautovariables.h +++ b/lib/checkautovariables.h @@ -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); diff --git a/lib/checkbool.h b/lib/checkbool.h index 94d1714a9..3c185095c 100644 --- a/lib/checkbool.h +++ b/lib/checkbool.h @@ -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); diff --git a/lib/checkboost.h b/lib/checkboost.h index 376b137d5..1aba5b6d5 100644 --- a/lib/checkboost.h +++ b/lib/checkboost.h @@ -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 { diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index 16e3a7532..f83fd928b 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -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 &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override; -private: - void arrayIndex(); void arrayIndexError(const Token* tok, const std::vector& dimensions, diff --git a/lib/checkclass.h b/lib/checkclass.h index 3134ebf15..a6b6732a2 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -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 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 &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override; - /** @brief Set of the STL types whose operator[] is not const */ - static const std::set stl_containers_not_const; - -private: const SymbolDatabase* mSymbolDatabase{}; // Reporting errors.. diff --git a/lib/checkcondition.h b/lib/checkcondition.h index 4c75a8a93..d1e29574f 100644 --- a/lib/checkcondition.h +++ b/lib/checkcondition.h @@ -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 mCondDiags; bool diag(const Token* tok, bool insert=true); diff --git a/lib/checkexceptionsafety.h b/lib/checkexceptionsafety.h index f70573c4b..e0a56c440 100644 --- a/lib/checkexceptionsafety.h +++ b/lib/checkexceptionsafety.h @@ -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); diff --git a/lib/checkfunctions.h b/lib/checkfunctions.h index 05502a870..a4cdd4fe8 100644 --- a/lib/checkfunctions.h +++ b/lib/checkfunctions.h @@ -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(); diff --git a/lib/checkinternal.h b/lib/checkinternal.h index e827d3c4e..823d3d2f5 100644 --- a/lib/checkinternal.h +++ b/lib/checkinternal.h @@ -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); diff --git a/lib/checkio.h b/lib/checkio.h index a1fdcd106..8fb109283 100644 --- a/lib/checkio.h +++ b/lib/checkio.h @@ -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); diff --git a/lib/checkleakautovar.h b/lib/checkleakautovar.h index 06243f513..8b44b915e 100644 --- a/lib/checkleakautovar.h +++ b/lib/checkleakautovar.h @@ -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(); diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index ca2adde63..c406ffccf 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -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. diff --git a/lib/checknullpointer.h b/lib/checknullpointer.h index e3b7c2714..1786f9804 100644 --- a/lib/checknullpointer.h +++ b/lib/checknullpointer.h @@ -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 &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 &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 &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); diff --git a/lib/checkother.h b/lib/checkother.h index 35aad044e..b1969a47d 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -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); diff --git a/lib/checkpostfixoperator.h b/lib/checkpostfixoperator.h index 99460cb31..2c163ecfc 100644 --- a/lib/checkpostfixoperator.h +++ b/lib/checkpostfixoperator.h @@ -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); diff --git a/lib/checksizeof.h b/lib/checksizeof.h index f47863c58..e738c8fce 100644 --- a/lib/checksizeof.h +++ b/lib/checksizeof.h @@ -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); diff --git a/lib/checkstl.h b/lib/checkstl.h index 8ec207cfd..dffacfe8b 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -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; diff --git a/lib/checkstring.h b/lib/checkstring.h index c5cf999c3..0b2cb4ac2 100644 --- a/lib/checkstring.h +++ b/lib/checkstring.h @@ -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); diff --git a/lib/checktype.h b/lib/checktype.h index c174691f6..dcab41b52 100644 --- a/lib/checktype.h +++ b/lib/checktype.h @@ -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 &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); diff --git a/lib/checkuninitvar.h b/lib/checkuninitvar.h index ea9e75ed7..8ab6fd9b8 100644 --- a/lib/checkuninitvar.h +++ b/lib/checkuninitvar.h @@ -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 &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 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 mUninitDiags; Check::FileInfo* getFileInfo() const; diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index b1dff174d..fad92637e 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -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); diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index e0e58cf5b..762300ccf 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -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 &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"); } diff --git a/lib/checkunusedvar.h b/lib/checkunusedvar.h index 843704f20..0275c5530 100644 --- a/lib/checkunusedvar.h +++ b/lib/checkunusedvar.h @@ -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); diff --git a/lib/checkvaarg.h b/lib/checkvaarg.h index e2f0ad928..2284e2bff 100644 --- a/lib/checkvaarg.h +++ b/lib/checkvaarg.h @@ -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);