diff --git a/lib/check.h b/lib/check.h index 1a6c57faa55..e98d1c7e31d 100644 --- a/lib/check.h +++ b/lib/check.h @@ -61,10 +61,12 @@ class CPPCHECKLIB Check { /** 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 @@ class CPPCHECKLIB Check { 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 e79acbd8567..8e9983ef7d8 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 @@ class CPPCHECKLIB Check64BitPortability : public Check { /** 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 e2455a921ce..bf84a5e9306 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 @@ class CPPCHECKLIB CheckAssert : public Check { 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 b02a0d0a6c3..fa4b1d92f08 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 @@ class CPPCHECKLIB CheckAutoVariables : public Check { 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 94d1714a97e..3c185095c7f 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 @@ class CPPCHECKLIB CheckBool : public Check { /** @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 1ea25b28501..c0317b144ee 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 @@ class CPPCHECKLIB CheckBoost : public Check { /** @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 16e3a7532da..f83fd928be7 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 @@ class CPPCHECKLIB CheckBufferOverrun : public Check { /** @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 3134ebf152f..a6b6732a224 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 @@ class CPPCHECKLIB CheckClass : public Check { /** @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 4c75a8a9352..d1e29574f3c 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 @@ class CPPCHECKLIB CheckCondition : public Check { /** @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 f70573c4bc2..e0a56c44058 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 @@ class CPPCHECKLIB CheckExceptionSafety : public Check { /** @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 05502a870f3..a4cdd4fe813 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 @@ class CPPCHECKLIB CheckFunctions : public Check { /** @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 e827d3c4eac..823d3d2f57c 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 @@ class CPPCHECKLIB CheckInternal : public Check { /** @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 a1fdcd10669..8fb109283b3 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 @@ class CPPCHECKLIB CheckIO : public Check { /** @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 06243f51341..8b44b915ef2 100644 --- a/lib/checkleakautovar.h +++ b/lib/checkleakautovar.h @@ -107,10 +107,13 @@ class CPPCHECKLIB VarInfo { */ 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 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check { checkLeakAutoVar.check(); } -private: - /** check for leaks in all scopes */ void check(); diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index ca2adde635c..c406ffccfff 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -163,11 +163,14 @@ class CPPCHECKLIB CheckMemoryLeak { * -# 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 @@ class CPPCHECKLIB CheckMemoryLeakInFunction : private Check, public CheckMemoryL */ 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 @@ class CPPCHECKLIB CheckMemoryLeakInFunction : private Check, public CheckMemoryL * @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 @@ class CPPCHECKLIB CheckMemoryLeakInClass : private Check, private CheckMemoryLea void check(); -private: void variable(const Scope *scope, const Token *tokVarname); /** Public functions: possible double-allocation */ @@ -262,10 +266,13 @@ class CPPCHECKLIB CheckMemoryLeakInClass : private Check, private CheckMemoryLea /** @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 @@ class CPPCHECKLIB CheckMemoryLeakStructMember : private Check, private CheckMemo void check(); -private: - /** Is local variable allocated with malloc? */ bool isMalloc(const Variable *variable) const; @@ -298,10 +303,13 @@ class CPPCHECKLIB CheckMemoryLeakStructMember : private Check, private CheckMemo /** @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 @@ class CPPCHECKLIB CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak 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 e3b7c2714be..1786f9804c3 100644 --- a/lib/checknullpointer.h +++ b/lib/checknullpointer.h @@ -47,22 +47,27 @@ 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) {} + /** + * Is there a pointer dereference? Everything that should result in + * a nullpointer dereference error message will result in a true + * return value. If it's unknown if the pointer is dereferenced false + * is returned. + * @param tok token for the pointer + * @param unknown it is not known if there is a pointer dereference (could be reported as a debug message) + * @return true => there is a dereference + */ + bool isPointerDeRef(const Token *tok, bool &unknown) const; - /** @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(); - } + 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 @@ -73,18 +78,17 @@ class CPPCHECKLIB CheckNullPointer : public Check { 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 - * return value. If it's unknown if the pointer is dereferenced false - * is returned. - * @param tok token for the pointer - * @param unknown it is not known if there is a pointer dereference (could be reported as a debug message) - * @return true => there is a dereference - */ - bool isPointerDeRef(const Token *tok, bool &unknown) const; + /** @brief This constructor is used when running checks. */ + CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) {} - static bool isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings); + /** @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 @@ class CPPCHECKLIB CheckNullPointer : public Check { /** @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 35aad044e85..b1969a47da7 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 @@ class CPPCHECKLIB CheckOther : public Check { 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 @@ class CPPCHECKLIB CheckOther : public Check { 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 500a38293f1..c48218743a5 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 @@ class CPPCHECKLIB CheckPostfixOperator : public Check { /** Check postfix operators */ void postfixOperator(); -private: /** Report Error */ void postfixOperatorError(const Token *tok); diff --git a/lib/checksizeof.h b/lib/checksizeof.h index f47863c582c..e738c8fceba 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 @@ class CPPCHECKLIB CheckSizeof : public Check { /** @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 8ec207cfd1d..dffacfe8bea 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 @@ class CPPCHECKLIB CheckStl : public Check { 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 c5cf999c3df..0b2cb4ac262 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 @@ class CPPCHECKLIB CheckString : public Check { /** @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 c174691f6dc..dcab41b5283 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 @@ class CPPCHECKLIB CheckType : public Check { 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 ea9e75ed7d8..8ab6fd9b8bd 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 @@ class CPPCHECKLIB CheckUninitVar : public Check { 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 @@ class CPPCHECKLIB CheckUninitVar : public Check { } 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 b1dff174d30..fad92637eb9 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 e0e58cf5b54..762300ccf10 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 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check { 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 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check { /** @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 843704f204d..0275c55303b 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 @@ class CPPCHECKLIB CheckUnusedVar : public Check { /** @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 cd509e0d0d7..bf8de27d99c 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 @@ class CPPCHECKLIB CheckVaarg : public Check { 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);