From c3ad3f78e90e24674137cfe685b21e0a920321f6 Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Wed, 2 Feb 2011 21:57:08 +1300 Subject: [PATCH 1/3] test case to check whether instances are sorted --- test/testcppcheck.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index af6e33883d7..3aaff3c3553 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -66,9 +66,21 @@ class TestCppcheck : public TestFixture void run() { + TEST_CASE(instancesSorted); TEST_CASE(getErrorMessages); } + void instancesSorted() + { + for (std::list::iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) { + std::list::iterator j = i; + ++j; + if (j != Check::instances().end()) { + ASSERT_EQUALS(true, (*i)->name() < (*j)->name()); + } + } + } + void getErrorMessages() { ErrorLogger2 errorLogger; From be195a72c97122c5685e818ea612e6e577141761 Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Wed, 2 Feb 2011 22:29:10 +1300 Subject: [PATCH 2/3] initialise Check::_name in constructor rather than relying on virtual Check::name() --- lib/check.h | 14 +++++++++----- lib/checkautovariables.h | 6 +++--- lib/checkbufferoverrun.h | 6 +++--- lib/checkclass.cpp | 2 +- lib/checkclass.h | 4 ++-- lib/checkexceptionsafety.h | 6 +++--- lib/checkmemoryleak.h | 24 ++++++++++++------------ lib/checknullpointer.h | 6 +++--- lib/checkobsoletefunctions.h | 6 +++--- lib/checkother.h | 6 +++--- lib/checkpostfixoperator.h | 6 +++--- lib/checkstl.h | 6 +++--- lib/checkuninitvar.h | 6 +++--- lib/checkunusedfunctions.h | 6 +++--- 14 files changed, 54 insertions(+), 50 deletions(-) diff --git a/lib/check.h b/lib/check.h index c11eb8f5d95..f5e5d637020 100644 --- a/lib/check.h +++ b/lib/check.h @@ -39,16 +39,16 @@ class Check { public: /** This constructor is used when registering the CheckClass */ - Check() - : _tokenizer(0), _settings(0), _errorLogger(0) + Check(const std::string &aname) + : _name(aname), _tokenizer(0), _settings(0), _errorLogger(0) { instances().push_back(this); instances().sort(); } /** This constructor is used when running checks. */ - Check(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger) + Check(const std::string &aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) + : _name(aname), _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger) { } virtual ~Check() @@ -98,7 +98,10 @@ class Check virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) = 0; /** class name, used to generate documentation */ - virtual std::string name() const = 0; + std::string name() const + { + return _name; + } /** get information about this class, used to generate documentation */ virtual std::string classInfo() const = 0; @@ -114,6 +117,7 @@ class Check } protected: + const std::string _name; const Tokenizer * const _tokenizer; const Settings * const _settings; ErrorLogger * const _errorLogger; diff --git a/lib/checkautovariables.h b/lib/checkautovariables.h index 97df17ccf94..a1faf14505e 100644 --- a/lib/checkautovariables.h +++ b/lib/checkautovariables.h @@ -34,12 +34,12 @@ class CheckAutoVariables : public Check { public: /** This constructor is used when registering the CheckClass */ - CheckAutoVariables() : Check() + CheckAutoVariables() : Check(myName()) { } /** This constructor is used when running checks. */ CheckAutoVariables(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) @@ -98,7 +98,7 @@ class CheckAutoVariables : public Check c.errorReturnTempPointer(0); } - std::string name() const + std::string myName() const { return "Auto Variables"; } diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index c2e77a20b3a..fe8ca26456c 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -50,12 +50,12 @@ class CheckBufferOverrun : public Check public: /** This constructor is used when registering the CheckClass */ - CheckBufferOverrun() : Check() + CheckBufferOverrun() : Check(myName()) { } /** This constructor is used when running checks. */ CheckBufferOverrun(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) @@ -209,7 +209,7 @@ class CheckBufferOverrun : public Check c.pointerOutOfBounds(0, "array"); } - std::string name() const + std::string myName() const { return "Bounds checking"; } diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index fc96af0cc8d..0991e795ffe 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -42,7 +42,7 @@ CheckClass instance; //--------------------------------------------------------------------------- CheckClass::CheckClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger), + : Check(myName(), tokenizer, settings, errorLogger), symbolDatabase(NULL) { diff --git a/lib/checkclass.h b/lib/checkclass.h index dd5f158839f..cc550c36bb5 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -36,7 +36,7 @@ class CheckClass : public Check { public: /** @brief This constructor is used when registering the CheckClass */ - CheckClass() : Check(), symbolDatabase(NULL) + CheckClass() : Check(myName()), symbolDatabase(NULL) { } /** @brief This constructor is used when running checks. */ @@ -144,7 +144,7 @@ class CheckClass : public Check c.checkConstError(0, "class", "function"); } - std::string name() const + std::string myName() const { return "Class"; } diff --git a/lib/checkexceptionsafety.h b/lib/checkexceptionsafety.h index 584cf78d6d9..af92597f5cc 100644 --- a/lib/checkexceptionsafety.h +++ b/lib/checkexceptionsafety.h @@ -43,12 +43,12 @@ class CheckExceptionSafety : public Check { public: /** This constructor is used when registering the CheckClass */ - CheckExceptionSafety() : Check() + CheckExceptionSafety() : Check(myName()) { } /** This constructor is used when running checks. */ CheckExceptionSafety(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } /** Checks that uses the simplified token list */ @@ -86,7 +86,7 @@ class CheckExceptionSafety : public Check } /** Short description of class (for --doc) */ - std::string name() const + std::string myName() const { return "Exception Safety"; } diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index d535ea4cb9c..7ba6a53b4da 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -172,12 +172,12 @@ class CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak { public: /** @brief This constructor is used when registering this class */ - CheckMemoryLeakInFunction() : Check(), CheckMemoryLeak(0, 0), symbolDatabase(NULL) + CheckMemoryLeakInFunction() : Check(myName()), CheckMemoryLeak(0, 0), symbolDatabase(NULL) { } /** @brief This constructor is used when running checks */ CheckMemoryLeakInFunction(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) - : Check(tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) + : Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) { // get the symbol database if (tokenizr) @@ -329,7 +329,7 @@ class CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak * Get name of class (--doc) * @return name of class */ - std::string name() const + std::string myName() const { return "Memory leaks (function variables)"; } @@ -364,11 +364,11 @@ class CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak class CheckMemoryLeakInClass : private Check, private CheckMemoryLeak { public: - CheckMemoryLeakInClass() : Check(), CheckMemoryLeak(0, 0) + CheckMemoryLeakInClass() : Check(myName()), CheckMemoryLeak(0, 0) { } CheckMemoryLeakInClass(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) - : Check(tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) + : Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) { } void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) @@ -396,7 +396,7 @@ class CheckMemoryLeakInClass : private Check, private CheckMemoryLeak void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) { } - std::string name() const + std::string myName() const { return "Memory leaks (class variables)"; } @@ -414,11 +414,11 @@ class CheckMemoryLeakInClass : private Check, private CheckMemoryLeak class CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak { public: - CheckMemoryLeakStructMember() : Check(), CheckMemoryLeak(0, 0) + CheckMemoryLeakStructMember() : Check(myName()), CheckMemoryLeak(0, 0) { } CheckMemoryLeakStructMember(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) - : Check(tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) + : Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) { } void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) @@ -434,7 +434,7 @@ class CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) { } - std::string name() const + std::string myName() const { return "Memory leaks (struct members)"; } @@ -452,11 +452,11 @@ class CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak class CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak { public: - CheckMemoryLeakNoVar() : Check(), CheckMemoryLeak(0, 0) + CheckMemoryLeakNoVar() : Check(myName()), CheckMemoryLeak(0, 0) { } CheckMemoryLeakNoVar(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) - : Check(tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) + : Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) { } void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) @@ -474,7 +474,7 @@ class CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) { } - std::string name() const + std::string myName() const { return "Memory leaks (address not taken)"; } diff --git a/lib/checknullpointer.h b/lib/checknullpointer.h index c1b134e9186..1f23bcf8717 100644 --- a/lib/checknullpointer.h +++ b/lib/checknullpointer.h @@ -37,12 +37,12 @@ class CheckNullPointer : public Check { public: /** @brief This constructor is used when registering the CheckNullPointer */ - CheckNullPointer() : Check() + CheckNullPointer() : Check(myName()) { } /** @brief This constructor is used when running checks. */ CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } /** @brief Run checks against the normal token list */ @@ -112,7 +112,7 @@ class CheckNullPointer : public Check } /** Name of check */ - std::string name() const + std::string myName() const { return "Null pointer"; } diff --git a/lib/checkobsoletefunctions.h b/lib/checkobsoletefunctions.h index 3ee4f88f375..706803aa889 100644 --- a/lib/checkobsoletefunctions.h +++ b/lib/checkobsoletefunctions.h @@ -38,14 +38,14 @@ class CheckObsoleteFunctions : public Check { public: /** This constructor is used when registering the CheckObsoleteFunctions */ - CheckObsoleteFunctions() : Check() + CheckObsoleteFunctions() : Check(myName()) { initObsoleteFunctions(); } /** This constructor is used when running checks. */ CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { initObsoleteFunctions(); } @@ -125,7 +125,7 @@ class CheckObsoleteFunctions : public Check } } - std::string name() const + std::string myName() const { return "Obsolete functions"; } diff --git a/lib/checkother.h b/lib/checkother.h index bace5a82e4e..bc5b1db350d 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -37,12 +37,12 @@ class CheckOther : public Check { public: /** @brief This constructor is used when registering the CheckClass */ - CheckOther() : Check() + CheckOther() : Check(myName()) { } /** @brief This constructor is used when running checks. */ CheckOther(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } /** @brief Run checks against the normal token list */ @@ -245,7 +245,7 @@ class CheckOther : public Check c.clarifyCalculationError(0); } - std::string name() const + std::string myName() const { return "Other"; } diff --git a/lib/checkpostfixoperator.h b/lib/checkpostfixoperator.h index 2840df19e7a..e7e4651fb22 100644 --- a/lib/checkpostfixoperator.h +++ b/lib/checkpostfixoperator.h @@ -35,12 +35,12 @@ class CheckPostfixOperator : public Check { public: /** This constructor is used when registering the CheckPostfixOperator */ - CheckPostfixOperator() : Check() + CheckPostfixOperator() : Check(myName()) { } /** This constructor is used when running checks. */ CheckPostfixOperator(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) @@ -62,7 +62,7 @@ class CheckPostfixOperator : public Check c.postfixOperatorError(0); } - std::string name() const + std::string myName() const { return "Using postfix operators"; } diff --git a/lib/checkstl.h b/lib/checkstl.h index e232eedcdfa..ebd6938b1e6 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -35,12 +35,12 @@ class CheckStl : public Check { public: /** This constructor is used when registering the CheckClass */ - CheckStl() : Check() + CheckStl() : Check(myName()) { } /** This constructor is used when running checks. */ CheckStl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } /** Simplified checks. The token list is simplified. */ @@ -173,7 +173,7 @@ class CheckStl : public Check c.redundantIfRemoveError(0); } - std::string name() const + std::string myName() const { return "STL usage"; } diff --git a/lib/checkuninitvar.h b/lib/checkuninitvar.h index 78465d82144..8ab4844edbb 100644 --- a/lib/checkuninitvar.h +++ b/lib/checkuninitvar.h @@ -37,12 +37,12 @@ class CheckUninitVar : public Check { public: /** @brief This constructor is used when registering the CheckUninitVar */ - CheckUninitVar() : Check() + CheckUninitVar() : Check(myName()) { } /** @brief This constructor is used when running checks. */ CheckUninitVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } /** @brief Run checks against the normal token list */ @@ -87,7 +87,7 @@ class CheckUninitVar : public Check c.uninitvarError(0, "varname"); } - std::string name() const + std::string myName() const { return "Uninitialized variables"; } diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index 066faafd636..6d408bcdb14 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -33,12 +33,12 @@ class CheckUnusedFunctions: public Check { public: /** @brief This constructor is used when registering the CheckUnusedFunctions */ - CheckUnusedFunctions() : Check() + CheckUnusedFunctions() : Check(myName()) { } /** @brief This constructor is used when running checks. */ CheckUnusedFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger) { } // Parse current tokens and determine.. @@ -69,7 +69,7 @@ class CheckUnusedFunctions: public Check } - std::string name() const + std::string myName() const { return "Unused functions"; } From bea36d1f83fea4035b9b4140144661945493feec Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Wed, 2 Feb 2011 22:56:14 +1300 Subject: [PATCH 3/3] use instances of less to compare pointers for list::sort() --- lib/check.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/check.h b/lib/check.h index f5e5d637020..c6e2bfa2554 100644 --- a/lib/check.h +++ b/lib/check.h @@ -39,12 +39,7 @@ class Check { public: /** This constructor is used when registering the CheckClass */ - Check(const std::string &aname) - : _name(aname), _tokenizer(0), _settings(0), _errorLogger(0) - { - instances().push_back(this); - instances().sort(); - } + Check(const std::string &aname); /** This constructor is used when running checks. */ Check(const std::string &aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) @@ -159,17 +154,28 @@ class Check private: - /** compare the names of Check classes, used when sorting the Check descendants */ - bool operator<(const Check *other) const - { - return (name() < other->name()); - } - /** disabled assignment operator */ void operator=(const Check &); }; +namespace std { + /** compare the names of Check classes, used when sorting the Check descendants */ + template <> struct less { + bool operator()(const Check *p1, const Check *p2) const + { + return (p1->name() < p2->name()); + } + }; +} + +inline Check::Check(const std::string &aname) + : _name(aname), _tokenizer(0), _settings(0), _errorLogger(0) +{ + instances().push_back(this); + instances().sort(std::less()); +} + /// @} #endif