From 07b661ef62aeacf4516c46afd0d7c4d74aa9bd54 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 7 Oct 2015 14:05:55 +0200 Subject: [PATCH] Refactorization: Improved handling of preprocessor in test suite - Set Preprocessor::macroChar to '$' once in the beginning to avoid problems when changing the order of tests - Removed Preprocessor usage from where it is not required - Fixed a TODO in testuninitvar.cpp --- test/testmemleak.cpp | 8 +- test/testother.cpp | 3 - test/testpreprocessor.cpp | 5 +- test/testrunner.cpp | 8 +- test/teststring.cpp | 178 +++++++++++++++----------------------- test/testuninitvar.cpp | 3 +- 6 files changed, 80 insertions(+), 125 deletions(-) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 451e266db..2b1ed9052 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -6523,15 +6523,9 @@ private: // Clear the error buffer.. errout.str(""); - // Preprocess... - Preprocessor preprocessor(settings, this); - std::istringstream istrpreproc(code); - std::map actual; - preprocessor.preprocess(istrpreproc, actual, "test.c"); - // Tokenize.. Tokenizer tokenizer(&settings, this); - std::istringstream istr(actual[""]); + std::istringstream istr(code); tokenizer.tokenize(istr, "test.c"); tokenizer.simplifyTokenList2(); diff --git a/test/testother.cpp b/test/testother.cpp index 91326de20..b97734ed5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4006,8 +4006,6 @@ private: } void duplicateBranch2() { - Preprocessor::macroChar = '$'; - check("void f(int x) {\n" // #4329 " if (x)\n" " $;\n" @@ -4428,7 +4426,6 @@ private: } void duplicateExpression5() { // #3749 - macros with same values - Preprocessor::macroChar = '$'; check("void f() {\n" " if ($a == $a) { }\n" "}"); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index b17b56e7b..ca304c2f7 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -41,7 +41,6 @@ class TestPreprocessor : public TestFixture { public: TestPreprocessor() : TestFixture("TestPreprocessor") { - Preprocessor::macroChar = '$'; } class OurPreprocessor : public Preprocessor { @@ -4010,9 +4009,9 @@ private: void macroChar() const { const char filedata[] = "#define X 1\nX\n"; ASSERT_EQUALS("\n$1\n", OurPreprocessor::expandMacros(filedata,nullptr)); - OurPreprocessor::macroChar = char(1); + Preprocessor::macroChar = char(1); ASSERT_EQUALS("\n" + std::string(char(1),1U) + "1\n", OurPreprocessor::expandMacros(filedata,nullptr)); - OurPreprocessor::macroChar = '$'; + Preprocessor::macroChar = '$'; } void validateCfg() { diff --git a/test/testrunner.cpp b/test/testrunner.cpp index 346956802..f276486fc 100644 --- a/test/testrunner.cpp +++ b/test/testrunner.cpp @@ -16,10 +16,12 @@ * along with this program. If not, see . */ +#include "testsuite.h" +#include "preprocessor.h" +#include "options.h" #include #include -#include "testsuite.h" -#include "options.h" +#include int main(int argc, char *argv[]) { @@ -31,6 +33,8 @@ int main(int argc, char *argv[]) #ifdef NDEBUG try { #endif + Preprocessor::macroChar = '$'; // While macroChar is char(1) per default outside test suite, we require it to be a human-readable character here. + options args(argc, const_cast(argv)); std::size_t failedTestsCount = TestFixture::runTests(args); diff --git a/test/teststring.cpp b/test/teststring.cpp index c9f960a8e..18123c526 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -20,7 +20,6 @@ #include "tokenize.h" #include "checkstring.h" #include "testsuite.h" -#include "preprocessor.h" #include "testutils.h" @@ -72,31 +71,6 @@ private: checkString.runSimplifiedChecks(&tokenizer, &settings, this); } - void check_preprocess_suppress(const char precode[]) { - // Clear the error buffer.. - errout.str(""); - - Settings settings; - settings.addEnabled("warning"); - - // Preprocess file.. - Preprocessor preprocessor(settings, this); - std::list configurations; - std::string filedata; - std::istringstream fin(precode); - preprocessor.preprocess(fin, filedata, configurations, "test.cpp", settings._includePaths); - const std::string code = preprocessor.getcode(filedata, "", "test.cpp"); - - // Tokenize.. - Tokenizer tokenizer(&settings, this); - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); - - // Check.. - CheckString checkString(&tokenizer, &settings, this); - checkString.checkAlwaysTrueOrFalseStringCompare(); - } - void stringLiteralWrite() { check("void f() {\n" " char *abc = \"abc\";\n" @@ -151,7 +125,7 @@ private: " if (!memcmp(p + offset, p, 42)){}\n" " if (!memcmp(offset + p, p, 42)){}\n" " if (!memcmp(p, offset + p, 42)){}\n" - "}\n"); + "}"); ASSERT_EQUALS("", errout.str()); // avoid false positives when the address is modified #6415 @@ -160,7 +134,7 @@ private: " if (!memcmp(c + offset, c, 42)){}\n" " if (!memcmp(offset + c, c, 42)){}\n" " if (!memcmp(c, offset + c, 42)){}\n" - "}\n"); + "}"); ASSERT_EQUALS("", errout.str()); // avoid false positives when the address is modified #6415 @@ -169,103 +143,91 @@ private: " if (!memcmp(s.c_str() + offset, s.c_str(), 42)){}\n" " if (!memcmp(offset + s.c_str(), s.c_str(), 42)){}\n" " if (!memcmp(s.c_str(), offset + s.c_str(), 42)){}\n" - "}\n"); + "}"); ASSERT_EQUALS("", errout.str()); - check_preprocess_suppress( - "#define MACRO \"00FF00\"\n" - "int main()\n" - "{\n" - " if (strcmp(MACRO,\"00FF00\") == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); - ASSERT_EQUALS("[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n", errout.str()); - - check_preprocess_suppress( - "int main()\n" - "{\n" - " if (stricmp(\"hotdog\",\"HOTdog\") == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); + check("int main()\n" + "{\n" + " if (strcmp(\"00FF00\", \"00FF00\") == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str()); - check_preprocess_suppress( - "#define MACRO \"Hotdog\"\n" - "int main()\n" - "{\n" - " if (QString::compare(\"Hamburger\", MACRO) == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); - ASSERT_EQUALS("[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n", errout.str()); + check("int main()\n" + "{\n" + " if (stricmp(\"hotdog\",\"HOTdog\") == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str()); - check_preprocess_suppress( - "int main()\n" - "{\n" - " if (QString::compare(argv[2], \"hotdog\") == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); + check("int main()\n" + "{\n" + " if (QString::compare(\"Hamburger\", \"Hotdog\") == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str()); + + check("int main()\n" + "{\n" + " if (QString::compare(argv[2], \"hotdog\") == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); ASSERT_EQUALS("", errout.str()); - check_preprocess_suppress( - "int main()\n" - "{\n" - " if (strncmp(\"hotdog\",\"hotdog\", 6) == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); + check("int main()\n" + "{\n" + " if (strncmp(\"hotdog\",\"hotdog\", 6) == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str()); - check( - "int foo(const char *buf)\n" - "{\n" - " if (strcmp(buf, buf) == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); + check("int foo(const char *buf)\n" + "{\n" + " if (strcmp(buf, buf) == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str()); - check( - "int foo(const std::string& buf)\n" - "{\n" - " if (stricmp(buf.c_str(), buf.c_str()) == 0)" - " {" - " std::cout << \"Equal\n\"" - " }" - "}"); + check("int foo(const std::string& buf)\n" + "{\n" + " if (stricmp(buf.c_str(), buf.c_str()) == 0)" + " {" + " std::cout << \"Equal\n\"" + " }" + "}"); ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str()); - check_preprocess_suppress( - "int main() {\n" - " if (\"str\" == \"str\") {\n" - " std::cout << \"Equal\n\"\n" - " }\n" - "}"); + check("int main() {\n" + " if (\"str\" == \"str\") {\n" + " std::cout << \"Equal\n\"\n" + " }\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n", errout.str()); - check_preprocess_suppress( - "int main() {\n" - " if (\"str\" != \"str\") {\n" - " std::cout << \"Equal\n\"\n" - " }\n" - "}"); + check("int main() {\n" + " if (\"str\" != \"str\") {\n" + " std::cout << \"Equal\n\"\n" + " }\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n", errout.str()); - check_preprocess_suppress( - "int main() {\n" - " if (a+\"str\" != \"str\"+b) {\n" - " std::cout << \"Equal\n\"\n" - " }\n" - "}"); + check("int main() {\n" + " if (a+\"str\" != \"str\"+b) {\n" + " std::cout << \"Equal\n\"\n" + " }\n" + "}"); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 7daff9948..7c1790733 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1989,8 +1989,7 @@ private: TODO_ASSERT_EQUALS("", "[test.cpp:3]: (error) Uninitialized variable: s\n", errout.str()); checkUninitVar("void f() {\n" - " #define w(x) ({ x z; (x*)z; })\n" // TODO: Preprocessor? - " int *n = w(typeof(*n));\n" + " int *n = ({ typeof(*n) z; (typeof(*n)*)z; })\n" "}", "test.cpp", false); ASSERT_EQUALS("", errout.str()); }