From 0be37fcf703ec70e9e3d63c5a6d078e690c25d03 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 6 Apr 2024 01:12:25 +0200 Subject: [PATCH] small `TestSimplifyTokens` cleanup --- Makefile | 4 +- test/helpers.h | 12 +++++ test/testfunctions.cpp | 13 +----- test/testsimplifytokens.cpp | 90 ++++++++++++++++--------------------- 4 files changed, 54 insertions(+), 65 deletions(-) diff --git a/Makefile b/Makefile index 32b030b8d266..a720612033f8 100644 --- a/Makefile +++ b/Makefile @@ -753,7 +753,7 @@ test/testexceptionsafety.o: test/testexceptionsafety.cpp lib/addoninfo.h lib/che test/testfilelister.o: test/testfilelister.cpp cli/filelister.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testfilelister.cpp -test/testfunctions.o: test/testfunctions.cpp lib/addoninfo.h lib/check.h lib/checkfunctions.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h +test/testfunctions.o: test/testfunctions.cpp lib/addoninfo.h lib/check.h lib/checkfunctions.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testfunctions.cpp test/testgarbage.o: test/testgarbage.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h @@ -816,7 +816,7 @@ test/testsettings.o: test/testsettings.cpp lib/addoninfo.h lib/check.h lib/color test/testsimplifytemplate.o: test/testsimplifytemplate.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsimplifytemplate.cpp -test/testsimplifytokens.o: test/testsimplifytokens.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h +test/testsimplifytokens.o: test/testsimplifytokens.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsimplifytokens.cpp test/testsimplifytypedef.o: test/testsimplifytypedef.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h diff --git a/test/helpers.h b/test/helpers.h index d134b6121d16..29f4f1cef808 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -170,4 +170,16 @@ struct make_default_obj } }; +inline std::string filter_valueflow(const std::string& s) { + std::istringstream istr(s); + std::ostringstream ostr; + std::string errline; + while (std::getline(istr, errline)) { + if (errline.find("valueflow.cpp") == std::string::npos) { + ostr << errline << '\n'; // TODO: last line might not contain a newline + } + } + return ostr.str(); +} + #endif // helpersH diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index a9227d636ff1..d907c9ae40ab 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -18,6 +18,7 @@ #include "checkfunctions.h" #include "errortypes.h" +#include "helpers.h" #include "settings.h" #include "standards.h" #include "fixture.h" @@ -104,18 +105,6 @@ class TestFunctions : public TestFixture { TEST_CASE(checkUseStandardLibrary14); } - static std::string filter_valueflow(const std::string& s) { - std::istringstream istr(s); - std::ostringstream ostr; - std::string errline; - while (std::getline(istr, errline)) { - if (errline.find("valueflow.cpp") == std::string::npos) { - ostr << errline << '\n'; - } - } - return ostr.str(); - } - #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) void check_(const char* file, int line, const char code[], const char filename[] = "test.cpp", const Settings* settings_ = nullptr) { if (!settings_) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0f46f1e39985..b946515d7f76 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -17,6 +17,7 @@ */ #include "errortypes.h" +#include "helpers.h" #include "platform.h" #include "settings.h" #include "standards.h" @@ -165,29 +166,27 @@ class TestSimplifyTokens : public TestFixture { } #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) - std::string tok_(const char* file, int line, const char code[], bool simplify = true, Platform::Type type = Platform::Type::Native) { + std::string tok_(const char* file, int line, const char code[], Platform::Type type = Platform::Type::Native) { const Settings settings = settingsBuilder(settings0).platform(type).build(); Tokenizer tokenizer(settings, this); std::istringstream istr(code); ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line); - return tokenizer.tokens()->stringifyList(nullptr, !simplify); + return tokenizer.tokens()->stringifyList(nullptr, false); } - std::string tok_(const char* file, int line, const char code[], const char filename[], bool simplify = true) { + std::string tok_(const char* file, int line, const char code[], const char filename[]) { Tokenizer tokenizer(settings0, this); std::istringstream istr(code); ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line); - (void)simplify; - return tokenizer.tokens()->stringifyList(nullptr, false); } #define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool simplify = false, bool expand = true, Platform::Type platform = Platform::Type::Native, const char* filename = "test.cpp", bool cpp11 = true) { + std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool expand = true, Platform::Type platform = Platform::Type::Native, const char* filename = "test.cpp", bool cpp11 = true) { const Settings settings = settingsBuilder(settings1).debugwarnings().platform(platform).cpp(cpp11 ? Standards::CPP11 : Standards::CPP03).build(); // tokenize.. @@ -195,31 +194,17 @@ class TestSimplifyTokens : public TestFixture { std::istringstream istr(code); ASSERT_LOC(tokenizer.tokenize(istr, filename), file, linenr); - (void)simplify; - - // TODO: should be handled in a better way - // filter out ValueFlow messages.. - const std::string debugwarnings = errout_str(); - std::istringstream istr2(debugwarnings); - std::string line; - while (std::getline(istr2,line)) { - if (line.find("bailout") == std::string::npos) - {} // TODO mErrout << line << "\n"; - } - if (tokenizer.tokens()) return tokenizer.tokens()->stringifyList(false, expand, false, true, false, nullptr, nullptr); return ""; } #define tokenizeDebugListing(...) tokenizeDebugListing_(__FILE__, __LINE__, __VA_ARGS__) - std::string tokenizeDebugListing_(const char* file, int line, const char code[], bool simplify = false, const char filename[] = "test.cpp") { + std::string tokenizeDebugListing_(const char* file, int line, const char code[], const char filename[] = "test.cpp") { Tokenizer tokenizer(settings0, this); std::istringstream istr(code); ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line); - (void)simplify; - // result.. return tokenizer.tokens()->stringifyList(true); } @@ -246,10 +231,10 @@ class TestSimplifyTokens : public TestFixture { ASSERT_EQUALS(tok(code2), tok(code1)); const char code3[] = "x = L\"1\" TEXT(\"2\") L\"3\";"; - ASSERT_EQUALS("x = L\"123\" ;", tok(code3, false, Platform::Type::Win64)); + ASSERT_EQUALS("x = L\"123\" ;", tok(code3, Platform::Type::Win64)); const char code4[] = "x = TEXT(\"1\") L\"2\";"; - ASSERT_EQUALS("x = L\"1\" L\"2\" ;", tok(code4, false, Platform::Type::Win64)); + ASSERT_EQUALS("x = L\"1\" L\"2\" ;", tok(code4, Platform::Type::Win64)); } void combine_wstrings() { @@ -1316,12 +1301,12 @@ class TestSimplifyTokens : public TestFixture { ASSERT_EQUALS("int f ( ) ;", tok("int __far __syscall f();")); ASSERT_EQUALS("int f ( ) ;", tok("int __far __pascal f();")); ASSERT_EQUALS("int f ( ) ;", tok("int __far __fortran f();")); - ASSERT_EQUALS("int f ( ) ;", tok("int WINAPI f();", true, Platform::Type::Win32A)); - ASSERT_EQUALS("int f ( ) ;", tok("int APIENTRY f();", true, Platform::Type::Win32A)); - ASSERT_EQUALS("int f ( ) ;", tok("int CALLBACK f();", true, Platform::Type::Win32A)); + ASSERT_EQUALS("int f ( ) ;", tok("int WINAPI f();", Platform::Type::Win32A)); + ASSERT_EQUALS("int f ( ) ;", tok("int APIENTRY f();", Platform::Type::Win32A)); + ASSERT_EQUALS("int f ( ) ;", tok("int CALLBACK f();", Platform::Type::Win32A)); // don't simplify Microsoft defines in unix code (#7554) - ASSERT_EQUALS("enum E { CALLBACK } ;", tok("enum E { CALLBACK } ;", true, Platform::Type::Unix32)); + ASSERT_EQUALS("enum E { CALLBACK } ;", tok("enum E { CALLBACK } ;", Platform::Type::Unix32)); } void simplifyAttribute() { @@ -1699,7 +1684,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 + v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1717,7 +1702,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 - v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1735,7 +1720,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 * v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1753,7 +1738,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 / v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1771,7 +1756,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 & v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1789,7 +1774,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 | v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1807,7 +1792,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 ^ v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1825,7 +1810,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 % v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1843,7 +1828,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 >> v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1861,7 +1846,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 << v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1879,7 +1864,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 == v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1897,7 +1882,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 != v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1915,7 +1900,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 > v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1933,7 +1918,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 >= v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1951,7 +1936,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 < v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1969,7 +1954,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 <= v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -1987,7 +1972,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 && v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } { @@ -2005,7 +1990,7 @@ class TestSimplifyTokens : public TestFixture { "4:\n" "5: return u@1 || v@2 ;\n" "6: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, true)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } } @@ -2060,7 +2045,7 @@ class TestSimplifyTokens : public TestFixture { "strcpy ( a , \"hello\" ) ;\n" "strcat ( a , \"!\" ) ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Platform::Type::Native, "test.c")); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, "test.c")); } { @@ -2102,6 +2087,7 @@ class TestSimplifyTokens : public TestFixture { "if ( s [ 6 ] == ' ' ) { ; }\n" "}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); + ASSERT_EQUALS("", filter_valueflow(errout_str())); } } @@ -2156,7 +2142,7 @@ class TestSimplifyTokens : public TestFixture { "cin >> x ;\n" "return x ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Platform::Type::Native, "test.cpp")); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, "test.cpp")); } } @@ -2170,7 +2156,7 @@ class TestSimplifyTokens : public TestFixture { "int x ; x = 0 ;\n" "cin >> std :: hex >> x ;\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Platform::Type::Native, "test.cpp")); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, "test.cpp")); } void simplifyKnownVariables48() { @@ -2183,7 +2169,7 @@ class TestSimplifyTokens : public TestFixture { "int i ;\n" "for ( i = 0 ; ( i < sz ) && ( sz > 3 ) ; ++ i ) { }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Platform::Type::Native, "test.c")); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, "test.c")); } void simplifyKnownVariables49() { // #3691 @@ -2199,7 +2185,7 @@ class TestSimplifyTokens : public TestFixture { "case 2 : ; x = sz ; break ;\n" "}\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Platform::Type::Native, "test.c")); + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, Platform::Type::Native, "test.c")); } void simplifyKnownVariables50() { // #4066 @@ -2340,6 +2326,7 @@ class TestSimplifyTokens : public TestFixture { "return i ;\n" "}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + ASSERT_EQUALS("", filter_valueflow(errout_str())); } void simplifyKnownVariablesBailOutAssign2() { @@ -2413,7 +2400,7 @@ class TestSimplifyTokens : public TestFixture { " return a;\n" "}\n"; tokenizeAndStringify(code,true); - ASSERT_EQUALS("", errout_str()); // no debug warnings + ASSERT_EQUALS("", filter_valueflow(errout_str())); // no debug warnings } void simplifyKnownVariablesBailOutSwitchBreak() { @@ -2475,6 +2462,7 @@ class TestSimplifyTokens : public TestFixture { " while (!x) { dostuff(); }" "}"; ASSERT_EQUALS("static int x ; void f ( ) { x = 123 ; while ( ! x ) { dostuff ( ) ; } }", tokenizeAndStringify(code,true)); + ASSERT_EQUALS("", filter_valueflow(errout_str())); } void simplifyKnownVariablesNamespace() {