From 999bc80603aa524d6bcc9db6142dc08253043d97 Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 11 Apr 2024 12:59:40 +0200 Subject: [PATCH] testrunner: moved `Preprocessor` object into `PreprocessorHelper` --- test/helpers.cpp | 15 ++- test/helpers.h | 6 +- test/testpreprocessor.cpp | 184 +++++++++++++++-------------------- test/testtokenize.cpp | 3 +- test/testunusedfunctions.cpp | 4 +- 5 files changed, 92 insertions(+), 120 deletions(-) diff --git a/test/helpers.cpp b/test/helpers.cpp index d3053eacabee..51730b444e9f 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -111,27 +111,28 @@ ScopedFile::~ScopedFile() { } // TODO: we should be using the actual Preprocessor implementation -std::string PreprocessorHelper::getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression) +std::string PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression) { - std::map cfgcode = getcode(preprocessor, filedata.c_str(), std::set{cfg}, filename, inlineSuppression); + std::map cfgcode = getcode(settings, errorlogger, filedata.c_str(), std::set{cfg}, filename, inlineSuppression); const auto it = cfgcode.find(cfg); if (it == cfgcode.end()) return ""; return it->second; } -std::map PreprocessorHelper::getcode(Preprocessor &preprocessor, const char code[], const std::string &filename, SuppressionList *inlineSuppression) +std::map PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &filename, SuppressionList *inlineSuppression) { - return getcode(preprocessor, code, {}, filename, inlineSuppression); + return getcode(settings, errorlogger, code, {}, filename, inlineSuppression); } -std::map PreprocessorHelper::getcode(Preprocessor &preprocessor, const char code[], std::set cfgs, const std::string &filename, SuppressionList *inlineSuppression) +std::map PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::set cfgs, const std::string &filename, SuppressionList *inlineSuppression) { simplecpp::OutputList outputList; std::vector files; std::istringstream istr(code); simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList); + Preprocessor preprocessor(settings, errorlogger); if (inlineSuppression) preprocessor.inlineSuppressions(tokens, *inlineSuppression); tokens.removeComments(); @@ -155,10 +156,6 @@ std::map PreprocessorHelper::getcode(Preprocessor &pre } } - // Since "files" is a local variable the tracking info must be cleared.. - preprocessor.mMacroUsage.clear(); - preprocessor.mIfCond.clear(); - return cfgcode; } diff --git a/test/helpers.h b/test/helpers.h index edca52510fa5..99618f78b642 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -148,14 +148,14 @@ class PreprocessorHelper * @param filename name of source file * @param inlineSuppression the inline suppressions */ - static std::string getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr); - static std::map getcode(Preprocessor &preprocessor, const char code[], const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr); + static std::string getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr); + static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr); static void preprocess(const char code[], std::vector &files, Tokenizer& tokenizer, ErrorLogger& errorlogger); static void preprocess(const char code[], std::vector &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui); private: - static std::map getcode(Preprocessor &preprocessor, const char code[], std::set cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr); + static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::set cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr); }; namespace cppcheck { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index d50a21f31e9a..0d9833dfbeb6 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -69,7 +69,6 @@ class TestPreprocessor : public TestFixture { private: const Settings settings0 = settingsBuilder().severity(Severity::information).build(); - Preprocessor preprocessor0{settings0, *this}; void run() override { @@ -289,7 +288,7 @@ class TestPreprocessor : public TestFixture { { // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata, "file.cpp"); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata, "file.cpp"); // Compare results.. ASSERT_EQUALS(1U, actual.size()); @@ -299,7 +298,7 @@ class TestPreprocessor : public TestFixture { { // Ticket #7102 - skip __cplusplus in C code // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata, "file.c"); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata, "file.c"); // Compare results.. ASSERT_EQUALS(1U, actual.size()); @@ -330,9 +329,8 @@ class TestPreprocessor : public TestFixture { void error3() { const auto settings = dinit(Settings, $.userDefines = "__cplusplus"); - Preprocessor preprocessor(settings, *this); const std::string code("#error hello world!\n"); - PreprocessorHelper::getcode(preprocessor, code, "X", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "X", "test.c"); ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout_str()); } @@ -341,18 +339,16 @@ class TestPreprocessor : public TestFixture { // In included file { const auto settings = dinit(Settings, $.userDefines = "TEST"); - Preprocessor preprocessor(settings, *this); const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile"); - PreprocessorHelper::getcode(preprocessor, code, "TEST", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "TEST", "test.c"); ASSERT_EQUALS("[ab.h:1]: (error) #error hello world!\n", errout_str()); } // After including a file { const auto settings = dinit(Settings, $.userDefines = "TEST"); - Preprocessor preprocessor(settings, *this); const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa"); - PreprocessorHelper::getcode(preprocessor, code, "TEST", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "TEST", "test.c"); ASSERT_EQUALS("[test.c:2]: (error) #error aaa\n", errout_str()); } } @@ -362,9 +358,8 @@ class TestPreprocessor : public TestFixture { const auto settings = dinit(Settings, $.userDefines = "TEST", $.force = true); - Preprocessor preprocessor(settings, *this); const std::string code("#error hello world!\n"); - PreprocessorHelper::getcode(preprocessor, code, "X", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "X", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -487,7 +482,7 @@ class TestPreprocessor : public TestFixture { "int main() {}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Expected configurations: "" and "ABC" ASSERT_EQUALS(2, actual.size()); @@ -705,7 +700,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => don't crash.. - (void)PreprocessorHelper::getcode(preprocessor0, filedata); + (void)PreprocessorHelper::getcode(settings0, *this, filedata); } void if_cond11() { @@ -714,7 +709,7 @@ class TestPreprocessor : public TestFixture { "#elif FLT_MANT_DIG < W_TYPE_SIZE\n" "#endif\n" "#endif\n"; - (void)PreprocessorHelper::getcode(preprocessor0, filedata); + (void)PreprocessorHelper::getcode(settings0, *this, filedata); ASSERT_EQUALS("", errout_str()); } @@ -776,7 +771,7 @@ class TestPreprocessor : public TestFixture { "#endif\n" "#if YYDEBUG\n" "#endif\n"; - (void)PreprocessorHelper::getcode(preprocessor0, code); + (void)PreprocessorHelper::getcode(settings0, *this, code); // There's nothing to assert. It just needs to not hang. } @@ -788,7 +783,7 @@ class TestPreprocessor : public TestFixture { "#if !defined(_WIN32)\n" "#endif\n" "INLINE inline __forceinline\n"; - const std::map actual = PreprocessorHelper::getcode(preprocessor0, code); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, code); // First, it must not hang. Second, inline must becomes inline, and __forceinline must become __forceinline. ASSERT_EQUALS("\n\n\n\n\n$__forceinline $inline $__forceinline", actual.at("")); @@ -798,7 +793,7 @@ class TestPreprocessor : public TestFixture { const char* code = "__asm__ \n" "{ int extern __value) 0; (double return (\"\" } extern\n" "__typeof __finite (__finite) __finite __inline \"__GI___finite\");"; - (void)PreprocessorHelper::getcode(preprocessor0, code); + (void)PreprocessorHelper::getcode(settings0, *this, code); } void macro_simple1() const { @@ -1133,7 +1128,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1173,7 +1168,7 @@ class TestPreprocessor : public TestFixture { "#undef z\n" "int z;\n" "z = 0;\n"; - ASSERT_EQUALS("\n\nint z ;\nz = 0 ;", PreprocessorHelper::getcode(preprocessor0, filedata, "", "")); + ASSERT_EQUALS("\n\nint z ;\nz = 0 ;", PreprocessorHelper::getcode(settings0, *this, filedata, "", "")); } } @@ -1235,7 +1230,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1295,7 +1290,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1358,7 +1353,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1376,7 +1371,7 @@ class TestPreprocessor : public TestFixture { "bbb"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1390,7 +1385,7 @@ class TestPreprocessor : public TestFixture { "bbb"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1404,7 +1399,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(2, actual.size()); @@ -1424,7 +1419,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); ASSERT_EQUALS(0, actual.size()); ASSERT_EQUALS("[file.c:2]: (error) No pair for character ('). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout_str()); @@ -1508,7 +1503,7 @@ class TestPreprocessor : public TestFixture { " }\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1525,7 +1520,7 @@ class TestPreprocessor : public TestFixture { "N"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(2, actual.size()); @@ -1544,7 +1539,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1560,7 +1555,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1577,7 +1572,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(2, actual.size()); @@ -1591,14 +1586,14 @@ class TestPreprocessor : public TestFixture { "#if A\n" "FOO\n" "#endif"; - ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } { const char filedata[] = "#define A 1\n" "#if A==1\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } } @@ -1608,7 +1603,7 @@ class TestPreprocessor : public TestFixture { "#if (B==A) || (B==C)\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } void define_if3() { @@ -1616,7 +1611,7 @@ class TestPreprocessor : public TestFixture { "#if (A==0)\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } void define_if4() { @@ -1624,7 +1619,7 @@ class TestPreprocessor : public TestFixture { "#if X==123\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } void define_if5() { // #4516 - #define B (A & 0x00f0) @@ -1634,7 +1629,7 @@ class TestPreprocessor : public TestFixture { "#if B==0x0010\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } { const char filedata[] = "#define A 0x00f0\n" @@ -1643,14 +1638,14 @@ class TestPreprocessor : public TestFixture { "#if C==0x0010\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\n\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } { const char filedata[] = "#define A (1+A)\n" // don't hang for recursive macros "#if A==1\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"","")); + ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(settings0, *this, filedata,"","")); } } @@ -1666,10 +1661,10 @@ class TestPreprocessor : public TestFixture { "#if B >= 0\n" "456\n" "#endif\n"; - const std::string actualA0 = PreprocessorHelper::getcode(preprocessor0, filedata, "A=0", "test.c"); + const std::string actualA0 = PreprocessorHelper::getcode(settings0, *this, filedata, "A=0", "test.c"); ASSERT_EQUALS(true, actualA0.find("123") != std::string::npos); ASSERT_EQUALS(false, actualA0.find("456") != std::string::npos); - const std::string actualA1 = PreprocessorHelper::getcode(preprocessor0, filedata, "A=1", "test.c"); + const std::string actualA1 = PreprocessorHelper::getcode(settings0, *this, filedata, "A=1", "test.c"); ASSERT_EQUALS(false, actualA1.find("123") != std::string::npos); ASSERT_EQUALS(true, actualA1.find("456") != std::string::npos); } @@ -1684,7 +1679,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, (int)actual.size()); @@ -1698,7 +1693,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, (int)actual.size()); @@ -1712,7 +1707,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, (int)actual.size()); @@ -1726,7 +1721,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, (int)actual.size()); @@ -1741,7 +1736,7 @@ class TestPreprocessor : public TestFixture { "A\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, (int)actual.size()); @@ -1756,7 +1751,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1U, actual.size()); @@ -1773,8 +1768,8 @@ class TestPreprocessor : public TestFixture { "B me;\n"; // Preprocess => actual result.. - ASSERT_EQUALS("\n\n\n\n\n\n$int me ;", PreprocessorHelper::getcode(preprocessor0, filedata, "", "a.cpp")); - ASSERT_EQUALS("\n\n\n\n\n\n$char me ;", PreprocessorHelper::getcode(preprocessor0, filedata, "A", "a.cpp")); + ASSERT_EQUALS("\n\n\n\n\n\n$int me ;", PreprocessorHelper::getcode(settings0, *this, filedata, "", "a.cpp")); + ASSERT_EQUALS("\n\n\n\n\n\n$char me ;", PreprocessorHelper::getcode(settings0, *this, filedata, "A", "a.cpp")); } void ifndef_define() { @@ -1784,7 +1779,7 @@ class TestPreprocessor : public TestFixture { "A(123);"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); ASSERT_EQUALS(1U, actual.size()); ASSERT_EQUALS("\n\n\n123 ;", actual.at("")); @@ -1797,8 +1792,8 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, filedata, "", "a.cpp")); - ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, filedata, "A", "a.cpp")); + ASSERT_EQUALS("", PreprocessorHelper::getcode(settings0, *this, filedata, "", "a.cpp")); + ASSERT_EQUALS("", PreprocessorHelper::getcode(settings0, *this, filedata, "A", "a.cpp")); } void redundant_config() { @@ -1818,7 +1813,7 @@ class TestPreprocessor : public TestFixture { // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(4, (int)actual.size()); @@ -1835,7 +1830,7 @@ class TestPreprocessor : public TestFixture { "#include \"notfound.h\"\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, (int)actual.size()); @@ -1853,7 +1848,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(preprocessor0, filedata); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); // B will always be defined if A is defined; the following test // cases should be fixed whenever this other bug is fixed @@ -1865,12 +1860,12 @@ class TestPreprocessor : public TestFixture { } void invalid_define_1() { - (void)PreprocessorHelper::getcode(preprocessor0, "#define =\n"); + (void)PreprocessorHelper::getcode(settings0, *this, "#define =\n"); ASSERT_EQUALS("[file.c:1]: (error) Failed to parse #define\n", errout_str()); } void invalid_define_2() { // #4036 - (void)PreprocessorHelper::getcode(preprocessor0, "#define () {(int f(x) }\n"); + (void)PreprocessorHelper::getcode(settings0, *this, "#define () {(int f(x) }\n"); ASSERT_EQUALS("[file.c:1]: (error) Failed to parse #define\n", errout_str()); } @@ -1878,14 +1873,13 @@ class TestPreprocessor : public TestFixture { /*const*/ Settings settings; settings.inlineSuppressions = true; settings.checks.enable(Checks::missingInclude); - Preprocessor preprocessor(settings, *this); const std::string code("// cppcheck-suppress missingInclude\n" "#include \"missing.h\"\n" "// cppcheck-suppress missingIncludeSystem\n" "#include \n"); SuppressionList inlineSuppr; - PreprocessorHelper::getcode(preprocessor, code, "", "test.c", &inlineSuppr); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c", &inlineSuppr); auto suppressions = inlineSuppr.getSuppressions(); ASSERT_EQUALS(2, suppressions.size()); @@ -1913,7 +1907,7 @@ class TestPreprocessor : public TestFixture { const std::string src("#if defined X || Y\n" "Fred & Wilma\n" "#endif\n"); - std::string actual = PreprocessorHelper::getcode(preprocessor0, src, "X=1", "test.c"); + std::string actual = PreprocessorHelper::getcode(settings0, *this, src, "X=1", "test.c"); ASSERT_EQUALS("\nFred & Wilma", actual); } @@ -1923,12 +1917,12 @@ class TestPreprocessor : public TestFixture { "Fred & Wilma\n" "#endif\n"); { - std::string actual = PreprocessorHelper::getcode(preprocessor0, src, "X=1", "test.c"); + std::string actual = PreprocessorHelper::getcode(settings0, *this, src, "X=1", "test.c"); ASSERT_EQUALS("", actual); } { - std::string actual = PreprocessorHelper::getcode(preprocessor0, src, "X=1;Y=2", "test.c"); + std::string actual = PreprocessorHelper::getcode(settings0, *this, src, "X=1;Y=2", "test.c"); ASSERT_EQUALS("\nFred & Wilma", actual); } } @@ -1940,34 +1934,34 @@ class TestPreprocessor : public TestFixture { "#if (X == Y)\n" "Fred & Wilma\n" "#endif\n"; - const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "TEST", "test.c"); + const std::string actual = PreprocessorHelper::getcode(settings0, *this, code, "TEST", "test.c"); ASSERT_EQUALS("\n\n\nFred & Wilma", actual); } void predefine4() { // #3577 const char code[] = "char buf[X];\n"; - const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "X=123", "test.c"); + const std::string actual = PreprocessorHelper::getcode(settings0, *this, code, "X=123", "test.c"); ASSERT_EQUALS("char buf [ $123 ] ;", actual); } void predefine5() { // #3737, #5119 - automatically define __cplusplus // #3737... const char code[] = "#ifdef __cplusplus\n123\n#endif"; - ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, code, "", "test.c")); - ASSERT_EQUALS("\n123", PreprocessorHelper::getcode(preprocessor0, code, "", "test.cpp")); + ASSERT_EQUALS("", PreprocessorHelper::getcode(settings0, *this, code, "", "test.c")); + ASSERT_EQUALS("\n123", PreprocessorHelper::getcode(settings0, *this, code, "", "test.cpp")); } void predefine6() { // automatically define __STDC_VERSION__ const char code[] = "#ifdef __STDC_VERSION__\n123\n#endif"; - ASSERT_EQUALS("\n123", PreprocessorHelper::getcode(preprocessor0, code, "", "test.c")); - ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, code, "", "test.cpp")); + ASSERT_EQUALS("\n123", PreprocessorHelper::getcode(settings0, *this, code, "", "test.c")); + ASSERT_EQUALS("", PreprocessorHelper::getcode(settings0, *this, code, "", "test.cpp")); } void invalidElIf() { // #2942 - segfault const char code[] = "#elif (){\n"; - const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "TEST", "test.c"); + const std::string actual = PreprocessorHelper::getcode(settings0, *this, code, "TEST", "test.c"); ASSERT_EQUALS("", actual); ASSERT_EQUALS("[test.c:1]: (error) #elif without #if\n", errout_str()); } @@ -2199,7 +2193,7 @@ class TestPreprocessor : public TestFixture { "#else\n" "#endif"; - const std::map actual = PreprocessorHelper::getcode(preprocessor0, code); + const std::map actual = PreprocessorHelper::getcode(settings0, *this, code); ASSERT_EQUALS("\nFred & Wilma", actual.at("")); } @@ -2216,7 +2210,7 @@ class TestPreprocessor : public TestFixture { "int x;\n"; // Preprocess => don't crash.. - (void)PreprocessorHelper::getcode(preprocessor0, filedata); + (void)PreprocessorHelper::getcode(settings0, *this, filedata); ASSERT_EQUALS( "[file.c:1]: (error) Syntax error in #ifdef\n" "[file.c:1]: (error) Syntax error in #ifdef\n", errout_str()); @@ -2228,14 +2222,13 @@ class TestPreprocessor : public TestFixture { "#if ! defined ( Y ) #endif"; // Preprocess => don't crash.. - (void)PreprocessorHelper::getcode(preprocessor0, filedata); + (void)PreprocessorHelper::getcode(settings0, *this, filedata); } void wrongPathOnErrorDirective() { const auto settings = dinit(Settings, $.userDefines = "foo"); - Preprocessor preprocessor(settings, *this); const std::string code("#error hello world!\n"); - PreprocessorHelper::getcode(preprocessor, code, "X", "./././test.c"); + PreprocessorHelper::getcode(settings, *this, code, "X", "./././test.c"); ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout_str()); } @@ -2246,12 +2239,11 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", ""); std::string code("#include \"header.h\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2263,10 +2255,9 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); std::string code("#include \"header.h\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); } @@ -2278,12 +2269,11 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", "", "inc"); std::string code("#include \"header.h\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); } @@ -2296,12 +2286,11 @@ class TestPreprocessor : public TestFixture { settings.includePaths.emplace_back("inc"); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", "", "inc"); std::string code("#include \"inc/header.h\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2314,12 +2303,11 @@ class TestPreprocessor : public TestFixture { settings.includePaths.emplace_back("inc"); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", "", Path::getCurrentPath()); std::string code("#include \"" + header.path() + "\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2331,12 +2319,11 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); const std::string header = Path::join(Path::getCurrentPath(), "header.h"); std::string code("#include \"" + header + "\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str()); } @@ -2348,12 +2335,11 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", ""); std::string code("#include "); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } @@ -2365,10 +2351,9 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); std::string code("#include "); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } @@ -2382,12 +2367,10 @@ class TestPreprocessor : public TestFixture { setTemplateFormat("simple"); settings.includePaths.emplace_back("system"); - Preprocessor preprocessor(settings, *this); - ScopedFile header("header.h", "", "system"); std::string code("#include "); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings0, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2400,12 +2383,11 @@ class TestPreprocessor : public TestFixture { settings.includePaths.emplace_back("inc"); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", "", Path::getCurrentPath()); std::string code("#include <" + header.path() + ">"); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2417,12 +2399,11 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); const std::string header = Path::join(Path::getCurrentPath(), "header.h"); std::string code("#include <" + header + ">"); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } @@ -2434,7 +2415,6 @@ class TestPreprocessor : public TestFixture { settings.checks.enable(Checks::missingInclude); settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); ScopedFile header("header.h", ""); ScopedFile header2("header2.h", ""); @@ -2443,7 +2423,7 @@ class TestPreprocessor : public TestFixture { "#include \n" "#include \n" "#include \"header2.h\""); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n" "test.c:2:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" @@ -2458,8 +2438,6 @@ class TestPreprocessor : public TestFixture { settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - Preprocessor preprocessor(settings, *this); - ScopedFile header("header.h", ""); ScopedFile header2("header2.h", ""); ScopedFile header3("header3.h", "", "system"); @@ -2481,7 +2459,7 @@ class TestPreprocessor : public TestFixture { "#include \"" + missing3 + "\"\n" "#include <" + header6.path() + ">\n" "#include <" + missing4 + ">\n"); - PreprocessorHelper::getcode(preprocessor, code, "", "test.c"); + PreprocessorHelper::getcode(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n" "test.c:2:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" @@ -2496,7 +2474,7 @@ class TestPreprocessor : public TestFixture { const char code[] = "void f(long l) {\n" " if (l > INT_MAX) {}\n" "}"; - const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "", "test.c"); + const std::string actual = PreprocessorHelper::getcode(settings0, *this, code, "", "test.c"); ASSERT_EQUALS("void f ( long l ) {\n" "if ( l > $2147483647 ) { }\n" "}", actual); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5536e40698f6..a61ff9dfc197 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6866,13 +6866,12 @@ class TestTokenizer : public TestFixture { "int PTR4 q4_var RBR4 = 0;\n"; // Preprocess file.. - Preprocessor preprocessor(settings0, *this); std::istringstream fin(raw_code); simplecpp::OutputList outputList; std::vector files; const simplecpp::TokenList tokens1(fin, files, emptyString, &outputList); const std::string filedata = tokens1.stringify(); - const std::string code = PreprocessorHelper::getcode(preprocessor, filedata, emptyString, emptyString); + const std::string code = PreprocessorHelper::getcode(settings0, *this, filedata, emptyString, emptyString); ASSERT_THROW_INTERNAL(tokenizeAndStringify(code.c_str()), AST); } diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 9ad7d655b5d5..dccb7538716d 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -21,7 +21,6 @@ #include "fixture.h" #include "helpers.h" #include "platform.h" -#include "preprocessor.h" #include "settings.h" #include "tokenize.h" @@ -682,8 +681,7 @@ class TestUnusedFunctions : public TestFixture { "};"; const char code[] = R"(#include "test.h")"; ScopedFile header("test.h", inc); - Preprocessor preprocessor(settings, *this); - const std::string processed = PreprocessorHelper::getcode(preprocessor, code, "", "test.cpp"); + const std::string processed = PreprocessorHelper::getcode(settings, *this, code, "", "test.cpp"); check(processed.c_str()); TODO_ASSERT_EQUALS("[test.h:3]: (style) The function 'f' is never used.\n", "[test.cpp:3]: (style) The function 'f' is never used.\n", errout_str()); }