From b211b8cbe8e78fe4316a282d57e8f6ba89876076 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 7 Feb 2009 20:06:00 +0000 Subject: [PATCH] Fix ticket #84 (unit testing: use "protected" instead of preprocessor) --- src/checkmemoryleak.h | 4 +- src/checkother.h | 4 +- src/preprocessor.h | 24 ++++++------ src/tokenize.h | 49 ++++++++++++------------- test/testbufferoverrun.cpp | 1 - test/testcharvar.cpp | 1 - test/testclass.cpp | 1 - test/testconstructors.cpp | 1 - test/testdangerousfunctions.cpp | 1 - test/testdivision.cpp | 1 - test/testfunctionusage.cpp | 1 - test/testincompletestatement.cpp | 2 +- test/testmemleak.cpp | 2 +- test/testmemleakmp.cpp | 18 +++++++-- test/testpreprocessor.cpp | 51 ++++++++++++++++---------- test/testredundantif.cpp | 18 ++++++++- test/testtokenize.cpp | 63 ++++++++++++++++++++++---------- test/testunusedprivfunc.cpp | 1 - test/testunusedvar.cpp | 1 - 19 files changed, 146 insertions(+), 98 deletions(-) diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index 3e22b630c..a661c0f1d 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -115,9 +115,7 @@ private: std::list _listAllocFunc; // Experimental functionality.. -#ifdef UNIT_TESTING -public: -#endif +protected: Token *functionParameterCode(const Token *ftok, int parameter); }; diff --git a/src/checkother.h b/src/checkother.h index d2e4b0206..3e43b5aa2 100644 --- a/src/checkother.h +++ b/src/checkother.h @@ -68,9 +68,7 @@ public: /** Returning pointer to local data */ void returnPointerToStackData(); -#ifndef UNIT_TESTING -private: -#endif +protected: void CheckVariableScope_LookupVar(const Token *tok1, const char varname[]); // Redundant condition diff --git a/src/preprocessor.h b/src/preprocessor.h index 455a62e67..ec69892a4 100644 --- a/src/preprocessor.h +++ b/src/preprocessor.h @@ -73,9 +73,19 @@ public: */ static std::string getcode(const std::string &filedata, std::string cfg); -#ifndef UNIT_TESTING +protected: + + /** + * Replace "#if defined" with "#ifdef" where possible + * + * @param str The string to be converted + * @return The replaced string + */ + static std::string replaceIfDefined(const std::string &str); + + static std::string expandMacros(std::string code); + private: -#endif /** * Remove space that has new line character on left or right side of it. @@ -85,14 +95,6 @@ private: */ static std::string removeSpaceNearNL(const std::string &str); - /** - * Replace "#if defined" with "#ifdef" where possible - * - * @param str The string to be converted - * @return The replaced string - */ - static std::string replaceIfDefined(const std::string &str); - /** * Get all possible configurations. By looking at the ifdefs and ifndefs in filedata */ @@ -102,8 +104,6 @@ private: static bool match_cfg_def(std::string cfg, const std::string &def); - static std::string expandMacros(std::string code); - /** * Search includes from code and append code from the included * file diff --git a/src/tokenize.h b/src/tokenize.h index ea3517978..525b675aa 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -75,11 +75,32 @@ public: const Token *GetFunctionTokenByName(const char funcname[]) const; const Token *tokens() const; +protected: + + /** Add braces to an if-block + * @return true if something is modified + * false if nothing is done. + */ + bool simplifyIfAddBraces(); + + /** Simplify casts + * @return true if something is modified + * false if nothing is done. + */ + bool simplifyCasts(); + + /** + * A simplify function that replaces a variable with its value in cases + * when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)" + * + * @return true if modifications to token-list are done. + * false if no modifications are done. + */ + bool simplifyKnownVariables(); + + std::vector _functionList; -#ifndef UNIT_TESTING private: -#endif - /** * Finds matching "end" for "start". @@ -92,12 +113,6 @@ private: void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); - /** Add braces to an if-block - * @return true if something is modified - * false if nothing is done. - */ - bool simplifyIfAddBraces(); - /** Simplify conditions * @return true if something is modified * false if nothing is done. @@ -111,27 +126,12 @@ private: */ bool removeReduntantConditions(); - /** Simplify casts - * @return true if something is modified - * false if nothing is done. - */ - bool simplifyCasts(); - /** Simplify function calls - constant return value * @return true if something is modified * false if nothing is done. */ bool simplifyFunctionReturn(); - /** - * A simplify function that replaces a variable with its value in cases - * when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)" - * - * @return true if modifications to token-list are done. - * false if no modifications are done. - */ - bool simplifyKnownVariables(); - /** * Remove redundant paranthesis: "((x))" => "(x)" * @return true if modifications to token-list are done. @@ -149,7 +149,6 @@ private: Token *_tokensBack; std::map _typeSize; - std::vector _functionList; std::vector _files; Token *_tokens; }; diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 38c4eee6b..4c6faa912 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -18,7 +18,6 @@ */ -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkbufferoverrun.h" #include "testsuite.h" diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index aa544ecd4..12d644f83 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -18,7 +18,6 @@ */ -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkother.h" #include "testsuite.h" diff --git a/test/testclass.cpp b/test/testclass.cpp index efe32360a..4170df75e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -19,7 +19,6 @@ -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkclass.h" #include "testsuite.h" diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 9af9b8e92..7cde9694d 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -19,7 +19,6 @@ -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkclass.h" #include "testsuite.h" diff --git a/test/testdangerousfunctions.cpp b/test/testdangerousfunctions.cpp index 35e79bcfe..c891747cf 100644 --- a/test/testdangerousfunctions.cpp +++ b/test/testdangerousfunctions.cpp @@ -18,7 +18,6 @@ */ -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkdangerousfunctions.h" #include "testsuite.h" diff --git a/test/testdivision.cpp b/test/testdivision.cpp index cde98f055..fe832650e 100644 --- a/test/testdivision.cpp +++ b/test/testdivision.cpp @@ -22,7 +22,6 @@ // such as "svar / uvar". Treating "svar" as unsigned data is not good -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkother.h" #include "testsuite.h" diff --git a/test/testfunctionusage.cpp b/test/testfunctionusage.cpp index 05a137b67..f4415e9c7 100644 --- a/test/testfunctionusage.cpp +++ b/test/testfunctionusage.cpp @@ -18,7 +18,6 @@ */ -#define UNIT_TESTING #include "../src/tokenize.h" #include "testsuite.h" #include "../src/checkfunctionusage.h" diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 0b3a0d062..337aca015 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -21,7 +21,7 @@ // Check for unused variables.. -#define UNIT_TESTING + #include "testsuite.h" #include "../src/tokenize.h" #include "../src/checkother.h" diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 492758a2b..155f2b036 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -20,7 +20,7 @@ -#define UNIT_TESTING + #include "../src/tokenize.h" #include "../src/checkmemoryleak.h" #include "testsuite.h" diff --git a/test/testmemleakmp.cpp b/test/testmemleakmp.cpp index 462d74717..4e419df6c 100644 --- a/test/testmemleakmp.cpp +++ b/test/testmemleakmp.cpp @@ -18,8 +18,6 @@ */ - -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkmemoryleak.h" #include "testsuite.h" @@ -33,6 +31,20 @@ public: TestMemleakMultiPass() : TestFixture("TestMemleakMultiPass") { } + class OurCheckMemoryLeakClass : public CheckMemoryLeakClass + { + public: + OurCheckMemoryLeakClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger) + : CheckMemoryLeakClass(tokenizer, settings, errorLogger) + { + } + + Token *functionParameterCode(const Token *ftok, int parameter) + { + return CheckMemoryLeakClass::functionParameterCode(ftok, parameter); + } + }; + private: void run() @@ -58,7 +70,7 @@ private: // Check.. Settings settings; - CheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this); + OurCheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this); Token *tok = checkMemoryLeak.functionParameterCode(tokenizer.tokens(), 1); // Compare tokens.. diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 8e3b21259..f019ecae3 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -23,7 +23,6 @@ #include "testsuite.h" -#define UNIT_TESTING #include "../src/preprocessor.h" #include @@ -35,6 +34,20 @@ public: TestPreprocessor() : TestFixture("TestPreprocessor") { } + class OurPreprocessor : public Preprocessor + { + public: + static std::string replaceIfDefined(const std::string &str) + { + return Preprocessor::replaceIfDefined(str); + } + + static std::string expandMacros(std::string code) + { + return Preprocessor::expandMacros(code); + } + }; + private: void run() @@ -473,7 +486,7 @@ private: std::string expected("#ifdef AAA\n#endif\n"); // Compare result.. - ASSERT_EQUALS(expected, Preprocessor::replaceIfDefined(filedata)); + ASSERT_EQUALS(expected, OurPreprocessor::replaceIfDefined(filedata)); } @@ -481,28 +494,28 @@ private: { const char filedata[] = "#define AAA(aa) f(aa)\n" "AAA(5);\n"; - ASSERT_EQUALS("\nf(5);\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nf(5);\n", OurPreprocessor::expandMacros(filedata)); } void macro_simple2() { const char filedata[] = "#define min(x,y) x 0 ) return 1;\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nif( temp > 0 ) return 1;\n", OurPreprocessor::expandMacros(filedata)); } void macro_simple5() @@ -514,21 +527,21 @@ private: " int temp = 0;\n" " ABC\n" "}\n"; - ASSERT_EQUALS("\n\nvoid foo()\n{\n int temp = 0;\n if( temp > 0 ) return 1;\n}\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\nvoid foo()\n{\n int temp = 0;\n if( temp > 0 ) return 1;\n}\n", OurPreprocessor::expandMacros(filedata)); } void macro_simple6() { const char filedata[] = "#define ABC (a+b+c)\n" "ABC"; - ASSERT_EQUALS("\n(a+b+c)", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n(a+b+c)", OurPreprocessor::expandMacros(filedata)); } void macro_simple7() { const char filedata[] = "#define ABC(str) str\n" "ABC(\"(\")"; - ASSERT_EQUALS("\n\"(\"", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\"(\"", OurPreprocessor::expandMacros(filedata)); } void macro_simple8() @@ -536,14 +549,14 @@ private: const char filedata[] = "#define ABC 123\n" "#define ABCD 1234\n" "ABC ABCD"; - ASSERT_EQUALS("\n\n123 1234", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n123 1234", OurPreprocessor::expandMacros(filedata)); } void macro_mismatch() { const char filedata[] = "#define AAA(aa,bb) f(aa)\n" "AAA(5);\n"; - ASSERT_EQUALS("\nAAA(5);\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nAAA(5);\n", OurPreprocessor::expandMacros(filedata)); } void string1() @@ -570,7 +583,7 @@ private: "str = \"AAA\""; // Compare results.. - ASSERT_EQUALS("\nstr = \"AAA\"", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nstr = \"AAA\"", OurPreprocessor::expandMacros(filedata)); } @@ -582,7 +595,7 @@ private: "AAA\n"; // Compare results.. - ASSERT_EQUALS("\n\n\nchar b=0;\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\nchar b=0;\n", OurPreprocessor::expandMacros(filedata)); } void defdef() @@ -593,7 +606,7 @@ private: "AAA\n"; // Compare results.. - ASSERT_EQUALS("\n\n\n789\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\n789\n", OurPreprocessor::expandMacros(filedata)); } void preprocessor_doublesharp() @@ -602,13 +615,13 @@ private: "TEST(foo,20);\n"; // Compare results.. - ASSERT_EQUALS("\nfoo=20;\n", Preprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nfoo=20;\n", OurPreprocessor::expandMacros(filedata)); const char filedata2[] = "#define TEST(var,val) var##_##val = val\n" "TEST(foo,20);\n"; // Compare results.. - ASSERT_EQUALS("\nfoo_20=20;\n", Preprocessor::expandMacros(filedata2)); + ASSERT_EQUALS("\nfoo_20=20;\n", OurPreprocessor::expandMacros(filedata2)); } @@ -641,7 +654,7 @@ private: "DBG(\"[0x%lx-0x%lx)\", pstart, pend);"; // Preprocess.. - std::string actual = Preprocessor::expandMacros(filedata); + std::string actual = OurPreprocessor::expandMacros(filedata); ASSERT_EQUALS("\nprintf(\"[0x%lx-0x%lx)\", pstart, pend);", actual); } @@ -652,7 +665,7 @@ private: "DBG(\"hello\");"; // Preprocess.. - std::string actual = Preprocessor::expandMacros(filedata); + std::string actual = OurPreprocessor::expandMacros(filedata); ASSERT_EQUALS("\nprintf(\"hello\");", actual); } @@ -688,7 +701,7 @@ private: "STRINGIFY(abc)"; // expand macros.. - std::string actual = Preprocessor::expandMacros(filedata); + std::string actual = OurPreprocessor::expandMacros(filedata); ASSERT_EQUALS("\n\"abc\"", actual); } diff --git a/test/testredundantif.cpp b/test/testredundantif.cpp index 7ce033ffe..8309747c9 100644 --- a/test/testredundantif.cpp +++ b/test/testredundantif.cpp @@ -22,7 +22,6 @@ // such as "svar / uvar". Treating "svar" as unsigned data is not good -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkother.h" #include "testsuite.h" @@ -37,6 +36,21 @@ public: TestRedundantIf() : TestFixture("TestRedundantIf") { } + class OurCheckOther : public CheckOther + { + public: + OurCheckOther(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger) + : CheckOther(tokenizer, settings, errorLogger) + { + + } + + void redundantCondition2() + { + CheckOther::redundantCondition2(); + } + }; + private: void check(const char code[]) { @@ -49,7 +63,7 @@ private: errout.str(""); // Check for redundant condition.. - CheckOther checkOther(&tokenizer, Settings(), this); + OurCheckOther checkOther(&tokenizer, Settings(), this); checkOther.redundantCondition2(); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index c5c269d86..32eac283d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -24,7 +24,6 @@ #include #include "testsuite.h" -#define UNIT_TESTING // Get access to "private" data in Tokenizer #include "../src/tokenize.h" extern std::ostringstream errout; @@ -34,6 +33,30 @@ public: TestTokenizer() : TestFixture("TestTokenizer") { } + class OurTokenizer : public Tokenizer + { + public: + void simplifyCasts() + { + Tokenizer::simplifyCasts(); + } + + bool simplifyIfAddBraces() + { + return Tokenizer::simplifyIfAddBraces(); + } + + bool simplifyKnownVariables() + { + return Tokenizer::simplifyKnownVariables(); + } + + std::vector &getFunctionList() + { + return _functionList; + } + }; + private: void run() @@ -153,7 +176,7 @@ private: const char code[] = "int *f(int *);"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -208,14 +231,14 @@ private: "void b()\n" "{ }\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); tokenizer.fillFunctionList(); - ASSERT_EQUALS(1, static_cast(tokenizer._functionList.size())); - ASSERT_EQUALS(std::string("b"), tokenizer._functionList[0]->aaaa()); + ASSERT_EQUALS(1, static_cast(tokenizer.getFunctionList().size())); + ASSERT_EQUALS(std::string("b"), tokenizer.getFunctionList()[0]->aaaa()); } void const_and_volatile_functions() @@ -239,18 +262,18 @@ private: // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); tokenizer.fillFunctionList(); - ASSERT_EQUALS(3, static_cast(tokenizer._functionList.size())); - if (tokenizer._functionList.size() == 3) + ASSERT_EQUALS(3, static_cast(tokenizer.getFunctionList().size())); + if (tokenizer.getFunctionList().size() == 3) { - ASSERT_EQUALS(std::string("a"), tokenizer._functionList[0]->str()); - ASSERT_EQUALS(std::string("b"), tokenizer._functionList[1]->str()); - ASSERT_EQUALS(std::string("c"), tokenizer._functionList[2]->str()); + ASSERT_EQUALS(std::string("a"), tokenizer.getFunctionList()[0]->str()); + ASSERT_EQUALS(std::string("b"), tokenizer.getFunctionList()[1]->str()); + ASSERT_EQUALS(std::string("c"), tokenizer.getFunctionList()[2]->str()); } } @@ -284,7 +307,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -304,7 +327,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -324,7 +347,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -349,7 +372,7 @@ private: // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -370,7 +393,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -393,7 +416,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -419,7 +442,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -441,7 +464,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); @@ -463,7 +486,7 @@ private: "}\n"; // tokenize.. - Tokenizer tokenizer; + OurTokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index e118a3e02..10b489f4d 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -18,7 +18,6 @@ */ -#define UNIT_TESTING #include "../src/tokenize.h" #include "../src/checkclass.h" #include "testsuite.h" diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 2f6b04a41..c84281ccf 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -21,7 +21,6 @@ // Check for unused variables.. -#define UNIT_TESTING #include "testsuite.h" #include "../src/tokenize.h" #include "../src/checkother.h"