diff --git a/test/helpers.cpp b/test/helpers.cpp index c9a463e76f4..3530854f5b5 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -27,11 +27,9 @@ #include #include -#include #include #include #include -#include #include #include #include @@ -47,8 +45,6 @@ #include "xml.h" -class SuppressionList; - const Settings SimpleTokenizer::s_settings; // TODO: better path-only usage @@ -115,53 +111,6 @@ ScopedFile::~ScopedFile() { } } -// TODO: we should be using the actual Preprocessor implementation -std::string PreprocessorHelper::getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression) -{ - std::map cfgcode = getcode(settings, errorlogger, filedata.c_str(), filedata.size(), std::set{cfg}, filename, inlineSuppression); - const auto it = cfgcode.find(cfg); - if (it == cfgcode.end()) - return ""; - return it->second; -} - -std::map PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &filename) -{ - return getcode(settings, errorlogger, code, size, {}, filename, nullptr); -} - -std::map PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, std::set cfgs, const std::string &filename, SuppressionList *inlineSuppression) -{ - simplecpp::OutputList outputList; - std::vector files; - - simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList); - Preprocessor preprocessor(settings, errorlogger, Path::identify(tokens.getFiles()[0], false)); - if (inlineSuppression) - preprocessor.inlineSuppressions(tokens, *inlineSuppression); - preprocessor.removeComments(tokens); - preprocessor.simplifyPragmaAsm(tokens); - - preprocessor.reportOutput(outputList, true); - - if (Preprocessor::hasErrors(outputList)) - return {}; - - std::map cfgcode; - if (cfgs.empty()) - cfgs = preprocessor.getConfigs(tokens); - for (const std::string & config : cfgs) { - try { - const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr); - cfgcode[config] = preprocessor.getcode(tokens, config, files, writeLocations); - } catch (const simplecpp::Output &) { - cfgcode[config] = ""; - } - } - - return cfgcode; -} - void SimpleTokenizer2::preprocess(const char* code, std::size_t size, std::vector &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger) { const simplecpp::TokenList tokens1(code, size, files, file0); diff --git a/test/helpers.h b/test/helpers.h index 1cecdc837a5..bb05598d8ea 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -27,15 +27,12 @@ #include "tokenlist.h" #include -#include -#include #include #include #include #include class Token; -class SuppressionList; class ErrorLogger; namespace tinyxml2 { class XMLDocument; @@ -168,31 +165,6 @@ class ScopedFile { const std::string mFullPath; }; -class PreprocessorHelper -{ -public: - /** - * Get preprocessed code for a given configuration - * - * Note: for testing only. - * - * @param filedata file data including preprocessing 'if', 'define', etc - * @param cfg configuration to read out - * @param filename name of source file - * @param inlineSuppression the inline suppressions - */ - static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr); - template - static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char (&code)[size], const std::string &filename = "file.c") - { - return getcode(settings, errorlogger, code, size-1, filename); - } - -private: - static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &filename); - static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, std::set cfgs, const std::string &filename, SuppressionList *inlineSuppression); -}; - namespace cppcheck { template std::size_t count_all_of(const std::string& str, T sub) { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 64b41c6891e..fc56f08df01 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -91,6 +91,60 @@ class TestPreprocessor : public TestFixture { return preprocessor.getRemarkComments(tokens1); } + static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr) + { + std::map cfgcode = getcode(settings, errorlogger, code, size, std::set{cfg}, filename, inlineSuppression); + const auto it = cfgcode.find(cfg); + if (it == cfgcode.end()) + return ""; + return it->second; + } + + template + static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const char (&code)[size], const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr) + { + return getcodeforcfg(settings, errorlogger, code, size-1, cfg, filename, inlineSuppression); + } + + template + static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char (&code)[size], const std::string &filename = "file.c") + { + return getcode(settings, errorlogger, code, size-1, {}, filename, nullptr); + } + + static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, std::set cfgs, const std::string &filename, SuppressionList *inlineSuppression) + { + simplecpp::OutputList outputList; + std::vector files; + + simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList); + // TODO: we should be using the actual Preprocessor implementation + Preprocessor preprocessor(settings, errorlogger, Path::identify(tokens.getFiles()[0], false)); + if (inlineSuppression) + preprocessor.inlineSuppressions(tokens, *inlineSuppression); + preprocessor.removeComments(tokens); + preprocessor.simplifyPragmaAsm(tokens); + + preprocessor.reportOutput(outputList, true); + + if (Preprocessor::hasErrors(outputList)) + return {}; + + std::map cfgcode; + if (cfgs.empty()) + cfgs = preprocessor.getConfigs(tokens); + for (const std::string & config : cfgs) { + try { + const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr); + cfgcode[config] = preprocessor.getcode(tokens, config, files, writeLocations); + } catch (const simplecpp::Output &) { + cfgcode[config] = ""; + } + } + + return cfgcode; + } + const Settings settings0 = settingsBuilder().severity(Severity::information).build(); void run() override { @@ -340,7 +394,7 @@ class TestPreprocessor : public TestFixture { { // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata, "file.cpp"); + const std::map actual = getcode(settings0, *this, filedata, "file.cpp"); // Compare results.. ASSERT_EQUALS(1U, actual.size()); @@ -350,7 +404,7 @@ class TestPreprocessor : public TestFixture { { // Ticket #7102 - skip __cplusplus in C code // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata, "file.c"); + const std::map actual = getcode(settings0, *this, filedata, "file.c"); // Compare results.. ASSERT_EQUALS(1U, actual.size()); @@ -381,8 +435,8 @@ class TestPreprocessor : public TestFixture { void error3() { const auto settings = dinit(Settings, $.userDefines = "__cplusplus"); - const std::string code("#error hello world!\n"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "X", "test.c"); + const char code[] = "#error hello world!\n"; + (void)getcodeforcfg(settings, *this, code, "X", "test.c"); ASSERT_EQUALS("[test.c:1:0]: (error) #error hello world! [preprocessorErrorDirective]\n", errout_str()); } @@ -391,16 +445,16 @@ class TestPreprocessor : public TestFixture { // In included file { const auto settings = dinit(Settings, $.userDefines = "TEST"); - const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "TEST", "test.c"); + const char code[] = "#file \"ab.h\"\n#error hello world!\n#endfile"; + (void)getcodeforcfg(settings, *this, code, "TEST", "test.c"); ASSERT_EQUALS("[ab.h:1:0]: (error) #error hello world! [preprocessorErrorDirective]\n", errout_str()); } // After including a file { const auto settings = dinit(Settings, $.userDefines = "TEST"); - const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "TEST", "test.c"); + const char code[] = "#file \"ab.h\"\n\n#endfile\n#error aaa"; + (void)getcodeforcfg(settings, *this, code, "TEST", "test.c"); ASSERT_EQUALS("[test.c:2:0]: (error) #error aaa [preprocessorErrorDirective]\n", errout_str()); } } @@ -410,8 +464,8 @@ class TestPreprocessor : public TestFixture { const auto settings = dinit(Settings, $.userDefines = "TEST", $.force = true); - const std::string code("#error hello world!\n"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "X", "test.c"); + const char code[] = "#error hello world!\n"; + (void)getcodeforcfg(settings, *this, code, "X", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -533,7 +587,7 @@ class TestPreprocessor : public TestFixture { "int main() {}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Expected configurations: "" and "ABC" ASSERT_EQUALS(2, actual.size()); @@ -751,7 +805,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => don't crash.. - (void)PreprocessorHelper::getcode(settings0, *this, filedata); + (void)getcode(settings0, *this, filedata); } void if_cond11() { @@ -760,7 +814,7 @@ class TestPreprocessor : public TestFixture { "#elif FLT_MANT_DIG < W_TYPE_SIZE\n" "#endif\n" "#endif\n"; - (void)PreprocessorHelper::getcode(settings0, *this, filedata); + (void)getcode(settings0, *this, filedata); ASSERT_EQUALS("", errout_str()); } @@ -822,7 +876,7 @@ class TestPreprocessor : public TestFixture { "#endif\n" "#if YYDEBUG\n" "#endif\n"; - (void)PreprocessorHelper::getcode(settings0, *this, code); + (void)getcode(settings0, *this, code); // There's nothing to assert. It just needs to not hang. } @@ -834,7 +888,7 @@ class TestPreprocessor : public TestFixture { "#if !defined(_WIN32)\n" "#endif\n" "INLINE inline __forceinline\n"; - const std::map actual = PreprocessorHelper::getcode(settings0, *this, code); + const std::map actual = 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("")); @@ -844,7 +898,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(settings0, *this, code); + (void)getcode(settings0, *this, code); } void macro_simple1() { @@ -1179,7 +1233,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1219,7 +1273,7 @@ class TestPreprocessor : public TestFixture { "#undef z\n" "int z;\n" "z = 0;\n"; - ASSERT_EQUALS("\n\nint z ;\nz = 0 ;", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata, "", "test.c")); + ASSERT_EQUALS("\n\nint z ;\nz = 0 ;", getcodeforcfg(settings0, *this, filedata, "", "test.c")); } } @@ -1281,7 +1335,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1341,7 +1395,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1404,7 +1458,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1422,7 +1476,7 @@ class TestPreprocessor : public TestFixture { "bbb"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1436,7 +1490,7 @@ class TestPreprocessor : public TestFixture { "bbb"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1450,7 +1504,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(2, actual.size()); @@ -1470,7 +1524,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); ASSERT_EQUALS(0, actual.size()); ASSERT_EQUALS("[file.c:2:0]: (error) No pair for character ('). Can't process file. File is either invalid or unicode, which is currently not supported. [preprocessorErrorDirective]\n", errout_str()); @@ -1554,7 +1608,7 @@ class TestPreprocessor : public TestFixture { " }\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1571,7 +1625,7 @@ class TestPreprocessor : public TestFixture { "N"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(2, actual.size()); @@ -1590,7 +1644,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1606,7 +1660,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1623,7 +1677,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(2, actual.size()); @@ -1637,14 +1691,14 @@ class TestPreprocessor : public TestFixture { "#if A\n" "FOO\n" "#endif"; - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("", getcodeforcfg(settings0, *this, filedata,"","test.c")); } { const char filedata[] = "#define A 1\n" "#if A==1\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } } @@ -1654,7 +1708,7 @@ class TestPreprocessor : public TestFixture { "#if (B==A) || (B==C)\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } void define_if3() { @@ -1662,7 +1716,7 @@ class TestPreprocessor : public TestFixture { "#if (A==0)\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } void define_if4() { @@ -1670,7 +1724,7 @@ class TestPreprocessor : public TestFixture { "#if X==123\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } void define_if5() { // #4516 - #define B (A & 0x00f0) @@ -1680,7 +1734,7 @@ class TestPreprocessor : public TestFixture { "#if B==0x0010\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } { const char filedata[] = "#define A 0x00f0\n" @@ -1689,14 +1743,14 @@ class TestPreprocessor : public TestFixture { "#if C==0x0010\n" "FOO\n" "#endif"; - ASSERT_EQUALS("\n\n\n\nFOO", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\n\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } { 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::getcodeforcfg(settings0, *this, filedata,"","test.c")); + ASSERT_EQUALS("\n\nFOO", getcodeforcfg(settings0, *this, filedata,"","test.c")); } } @@ -1712,10 +1766,10 @@ class TestPreprocessor : public TestFixture { "#if B >= 0\n" "456\n" "#endif\n"; - const std::string actualA0 = PreprocessorHelper::getcodeforcfg(settings0, *this, filedata, "A=0", "test.c"); + const std::string actualA0 = getcodeforcfg(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::getcodeforcfg(settings0, *this, filedata, "A=1", "test.c"); + const std::string actualA1 = getcodeforcfg(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); } @@ -1730,7 +1784,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1744,7 +1798,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1758,7 +1812,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1772,7 +1826,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1787,7 +1841,7 @@ class TestPreprocessor : public TestFixture { "A\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1802,7 +1856,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1U, actual.size()); @@ -1819,8 +1873,8 @@ class TestPreprocessor : public TestFixture { "B me;\n"; // Preprocess => actual result.. - ASSERT_EQUALS("\n\n\n\n\n\n$int me ;", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata, "", "a.cpp")); - ASSERT_EQUALS("\n\n\n\n\n\n$char me ;", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata, "A", "a.cpp")); + ASSERT_EQUALS("\n\n\n\n\n\n$int me ;", getcodeforcfg(settings0, *this, filedata, "", "a.cpp")); + ASSERT_EQUALS("\n\n\n\n\n\n$char me ;", getcodeforcfg(settings0, *this, filedata, "A", "a.cpp")); } void ifndef_define() { @@ -1830,7 +1884,7 @@ class TestPreprocessor : public TestFixture { "A(123);"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); ASSERT_EQUALS(1U, actual.size()); ASSERT_EQUALS("\n\n\n123 ;", actual.at("")); @@ -1843,8 +1897,8 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata, "", "a.cpp")); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings0, *this, filedata, "A", "a.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings0, *this, filedata, "", "a.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings0, *this, filedata, "A", "a.cpp")); } void redundant_config() { @@ -1864,7 +1918,7 @@ class TestPreprocessor : public TestFixture { // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(4, actual.size()); @@ -1881,7 +1935,7 @@ class TestPreprocessor : public TestFixture { "#include \"notfound.h\"\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = getcode(settings0, *this, filedata); // Compare results.. ASSERT_EQUALS(1, actual.size()); @@ -1899,7 +1953,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // Preprocess => actual result.. - const std::map actual = PreprocessorHelper::getcode(settings0, *this, filedata); + const std::map actual = 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 @@ -1911,12 +1965,12 @@ class TestPreprocessor : public TestFixture { } void invalid_define_1() { - (void)PreprocessorHelper::getcode(settings0, *this, "#define =\n"); + (void)getcode(settings0, *this, "#define =\n"); ASSERT_EQUALS("[file.c:1:0]: (error) Failed to parse #define [preprocessorErrorDirective]\n", errout_str()); } void invalid_define_2() { // #4036 - (void)PreprocessorHelper::getcode(settings0, *this, "#define () {(int f(x) }\n"); + (void)getcode(settings0, *this, "#define () {(int f(x) }\n"); ASSERT_EQUALS("[file.c:1:0]: (error) Failed to parse #define [preprocessorErrorDirective]\n", errout_str()); } @@ -1925,12 +1979,12 @@ class TestPreprocessor : public TestFixture { settings.inlineSuppressions = true; settings.checks.enable(Checks::missingInclude); - const std::string code("// cppcheck-suppress missingInclude\n" - "#include \"missing.h\"\n" - "// cppcheck-suppress missingIncludeSystem\n" - "#include \n"); + const char code[] = "// cppcheck-suppress missingInclude\n" + "#include \"missing.h\"\n" + "// cppcheck-suppress missingIncludeSystem\n" + "#include \n"; SuppressionList inlineSuppr; - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c", &inlineSuppr); + (void)getcodeforcfg(settings, *this, code, "", "test.c", &inlineSuppr); auto suppressions = inlineSuppr.getSuppressions(); ASSERT_EQUALS(2, suppressions.size()); @@ -1987,25 +2041,25 @@ class TestPreprocessor : public TestFixture { } void predefine1() { - const std::string src("#if defined X || Y\n" - "Fred & Wilma\n" - "#endif\n"); - std::string actual = PreprocessorHelper::getcodeforcfg(settings0, *this, src, "X=1", "test.c"); + const char code[] = "#if defined X || Y\n" + "Fred & Wilma\n" + "#endif\n"; + std::string actual = getcodeforcfg(settings0, *this, code, "X=1", "test.c"); ASSERT_EQUALS("\nFred & Wilma", actual); } void predefine2() { - const std::string src("#if defined(X) && Y\n" - "Fred & Wilma\n" - "#endif\n"); + const char code[] = "#if defined(X) && Y\n" + "Fred & Wilma\n" + "#endif\n"; { - std::string actual = PreprocessorHelper::getcodeforcfg(settings0, *this, src, "X=1", "test.c"); + std::string actual = getcodeforcfg(settings0, *this, code, "X=1", "test.c"); ASSERT_EQUALS("", actual); } { - std::string actual = PreprocessorHelper::getcodeforcfg(settings0, *this, src, "X=1;Y=2", "test.c"); + std::string actual = getcodeforcfg(settings0, *this, code, "X=1;Y=2", "test.c"); ASSERT_EQUALS("\nFred & Wilma", actual); } } @@ -2017,28 +2071,28 @@ class TestPreprocessor : public TestFixture { "#if (X == Y)\n" "Fred & Wilma\n" "#endif\n"; - const std::string actual = PreprocessorHelper::getcodeforcfg(settings0, *this, code, "TEST", "test.c"); + const std::string actual = getcodeforcfg(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::getcodeforcfg(settings0, *this, code, "X=123", "test.c"); + const std::string actual = getcodeforcfg(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::getcodeforcfg(settings0, *this, code, "", "test.c")); - ASSERT_EQUALS("\n123", PreprocessorHelper::getcodeforcfg(settings0, *this, code, "", "test.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings0, *this, code, "", "test.c")); + ASSERT_EQUALS("\n123", getcodeforcfg(settings0, *this, code, "", "test.cpp")); } void predefine6() { // automatically define __STDC_VERSION__ const char code[] = "#ifdef __STDC_VERSION__\n123\n#endif"; - ASSERT_EQUALS("\n123", PreprocessorHelper::getcodeforcfg(settings0, *this, code, "", "test.c")); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings0, *this, code, "", "test.cpp")); + ASSERT_EQUALS("\n123", getcodeforcfg(settings0, *this, code, "", "test.c")); + ASSERT_EQUALS("", getcodeforcfg(settings0, *this, code, "", "test.cpp")); } void strictAnsi() { @@ -2046,22 +2100,22 @@ class TestPreprocessor : public TestFixture { Settings settings; settings.standards.setStd("gnu99"); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c")); + ASSERT_EQUALS("", getcodeforcfg(settings, *this, code, "", "test.c")); settings.standards.setStd("c99"); - ASSERT_EQUALS("\n123", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c")); + ASSERT_EQUALS("\n123", getcodeforcfg(settings, *this, code, "", "test.c")); settings.standards.setStd("gnu++11"); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings, *this, code, "", "test.cpp")); settings.standards.setStd("c++11"); - ASSERT_EQUALS("\n123", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp")); + ASSERT_EQUALS("\n123", getcodeforcfg(settings, *this, code, "", "test.cpp")); } void invalidElIf() { // #2942 - segfault const char code[] = "#elif (){\n"; - const std::string actual = PreprocessorHelper::getcodeforcfg(settings0, *this, code, "TEST", "test.c"); + const std::string actual = getcodeforcfg(settings0, *this, code, "TEST", "test.c"); ASSERT_EQUALS("", actual); ASSERT_EQUALS("[test.c:1:0]: (error) #elif without #if [preprocessorErrorDirective]\n", errout_str()); } @@ -2293,7 +2347,7 @@ class TestPreprocessor : public TestFixture { "#else\n" "#endif"; - const std::map actual = PreprocessorHelper::getcode(settings0, *this, code); + const std::map actual = getcode(settings0, *this, code); ASSERT_EQUALS("\nFred & Wilma", actual.at("")); } @@ -2310,7 +2364,7 @@ class TestPreprocessor : public TestFixture { "int x;\n"; // Preprocess => don't crash.. - (void)PreprocessorHelper::getcode(settings0, *this, filedata); + (void)getcode(settings0, *this, filedata); ASSERT_EQUALS( "[file.c:1:0]: (error) Syntax error in #ifdef [preprocessorErrorDirective]\n" "[file.c:1:0]: (error) Syntax error in #ifdef [preprocessorErrorDirective]\n", errout_str()); @@ -2322,13 +2376,13 @@ class TestPreprocessor : public TestFixture { "#if ! defined ( Y ) #endif"; // Preprocess => don't crash.. - (void)PreprocessorHelper::getcode(settings0, *this, filedata); + (void)getcode(settings0, *this, filedata); } void wrongPathOnErrorDirective() { const auto settings = dinit(Settings, $.userDefines = "foo"); - const std::string code("#error hello world!\n"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "X", "./././test.c"); + const char code[] = "#error hello world!\n"; + (void)getcodeforcfg(settings, *this, code, "X", "./././test.c"); ASSERT_EQUALS("[test.c:1:0]: (error) #error hello world! [preprocessorErrorDirective]\n", errout_str()); } @@ -2342,8 +2396,8 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", ""); - std::string code("#include \"header.h\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include \"header.h\""; + (void)getcodeforcfg(settings, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2356,8 +2410,8 @@ class TestPreprocessor : public TestFixture { settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - std::string code("#include \"header.h\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include \"header.h\""; + (void)getcodeforcfg(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); } @@ -2372,8 +2426,8 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", "", "inc"); - std::string code("#include \"header.h\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include \"header.h\""; + (void)getcodeforcfg(settings, *this, code, "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); } @@ -2389,8 +2443,8 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", "", "inc"); - std::string code("#include \"inc/header.h\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include \"inc/header.h\""; + (void)getcodeforcfg(settings, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2407,7 +2461,7 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", "", Path::getCurrentPath()); std::string code("#include \"" + header.path() + "\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2423,7 +2477,7 @@ class TestPreprocessor : public TestFixture { const std::string header = Path::join(Path::getCurrentPath(), "header.h"); std::string code("#include \"" + header + "\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c"); ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str()); } @@ -2438,8 +2492,8 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", ""); - std::string code("#include "); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include "; + (void)getcodeforcfg(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()); } @@ -2452,8 +2506,8 @@ class TestPreprocessor : public TestFixture { settings.templateFormat = "simple"; // has no effect setTemplateFormat("simple"); - std::string code("#include "); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include "; + (void)getcodeforcfg(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()); } @@ -2469,8 +2523,8 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", "", "system"); - std::string code("#include "); - (void)PreprocessorHelper::getcodeforcfg(settings0, *this, code, "", "test.c"); + const char code[] = "#include "; + (void)getcodeforcfg(settings0, *this, code, "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2487,7 +2541,7 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", "", Path::getCurrentPath()); std::string code("#include <" + header.path() + ">"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c"); ASSERT_EQUALS("", errout_str()); } @@ -2503,7 +2557,7 @@ class TestPreprocessor : public TestFixture { const std::string header = Path::join(Path::getCurrentPath(), "header.h"); std::string code("#include <" + header + ">"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "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()); } @@ -2519,11 +2573,11 @@ class TestPreprocessor : public TestFixture { ScopedFile header("header.h", ""); ScopedFile header2("header2.h", ""); - std::string code("#include \"missing.h\"\n" - "#include \n" - "#include \n" - "#include \"header2.h\""); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + const char code[] = "#include \"missing.h\"\n" + "#include \n" + "#include \n" + "#include \"header2.h\""; + (void)getcodeforcfg(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" @@ -2559,7 +2613,7 @@ class TestPreprocessor : public TestFixture { "#include \"" + missing3 + "\"\n" "#include <" + header6.path() + ">\n" "#include <" + missing4 + ">\n"); - (void)PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.c"); + (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "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" @@ -2574,15 +2628,15 @@ class TestPreprocessor : public TestFixture { Settings settings; settings.standards.setStd("c++11"); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings, *this, code, "", "test.cpp")); ASSERT_EQUALS("[test.cpp:1:0]: (error) failed to evaluate #if condition, undefined function-like macro invocation: __has_include( ... ) [preprocessorErrorDirective]\n", errout_str()); settings.standards.setStd("c++17"); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings, *this, code, "", "test.cpp")); ASSERT_EQUALS("", errout_str()); settings.standards.setStd("gnu++11"); - ASSERT_EQUALS("", PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp")); + ASSERT_EQUALS("", getcodeforcfg(settings, *this, code, "", "test.cpp")); ASSERT_EQUALS("", errout_str()); } @@ -2591,7 +2645,7 @@ class TestPreprocessor : public TestFixture { const char code[] = "void f(long l) {\n" " if (l > INT_MAX) {}\n" "}"; - const std::string actual = PreprocessorHelper::getcodeforcfg(settings0, *this, code, "", "test.c"); + const std::string actual = getcodeforcfg(settings0, *this, code, "", "test.c"); ASSERT_EQUALS("void f ( long l ) {\n" "if ( l > $2147483647 ) { }\n" "}", actual); @@ -2666,7 +2720,7 @@ class TestPreprocessor : public TestFixture { "};"; const char code[] = R"(#include "test.h")"; ScopedFile header("test.h", inc); - const std::string processed = PreprocessorHelper::getcodeforcfg(settingsDefault, *this, code, "", "test.cpp"); + const std::string processed = getcodeforcfg(settingsDefault, *this, code, "", "test.cpp"); ASSERT_EQUALS( "\n" "#line 1 \"test.h\"\n"