From 8173aa8d699940f572669187a21c3ce9013a29d9 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 10 Jun 2024 18:19:53 +0300 Subject: [PATCH 1/3] Fix #12383: cppcheck build dir: changed line numbers in source file --- lib/preprocessor.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 738862e42eb..4ad0aea66ff 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -952,13 +952,17 @@ std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, con { std::string hashData = toolinfo; for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) { - if (!tok->comment) + if (!tok->comment) { hashData += tok->str(); + hashData += tok->location.line / tok->location.col; + } } for (std::map::const_iterator it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) { for (const simplecpp::Token *tok = it->second->cfront(); tok; tok = tok->next) { - if (!tok->comment) + if (!tok->comment) { hashData += tok->str(); + hashData += tok->location.line / tok->location.col; + } } } return (std::hash{})(hashData); From 204ed48dc7d2915f24943358378abc05896a3595 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 10 Jun 2024 21:17:33 +0300 Subject: [PATCH 2/3] Hash remodel --- lib/preprocessor.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 4ad0aea66ff..06d01cd1406 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -954,14 +954,16 @@ std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, con for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) { if (!tok->comment) { hashData += tok->str(); - hashData += tok->location.line / tok->location.col; + hashData += static_cast(tok->location.line); + hashData += static_cast(tok->location.col); } } for (std::map::const_iterator it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) { for (const simplecpp::Token *tok = it->second->cfront(); tok; tok = tok->next) { if (!tok->comment) { hashData += tok->str(); - hashData += tok->location.line / tok->location.col; + hashData += static_cast(tok->location.line); + hashData += static_cast(tok->location.col); } } } From 11e4d99fae278704ed18ae9b5b48851bffb9131b Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 11 Jun 2024 14:22:35 +0300 Subject: [PATCH 3/3] Add Test --- test/testpreprocessor.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index f3d0f45a56e..659bf930616 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -264,6 +264,8 @@ class TestPreprocessor : public TestFixture { TEST_CASE(testMissingIncludeCheckConfig); TEST_CASE(limitsDefines); + + TEST_CASE(hashCalculation); } std::string getConfigsStr(const char filedata[], const char *arg = nullptr) { @@ -284,6 +286,16 @@ class TestPreprocessor : public TestFixture { return ret; } + std::size_t getHash(const char filedata[]) { + Settings settings; + Preprocessor preprocessor(settings, *this); + std::vector files; + std::istringstream istr(filedata); + simplecpp::TokenList tokens(istr,files); + tokens.removeComments(); + return preprocessor.calculateHash(tokens, ""); + } + void Bug2190219() { const char filedata[] = "#ifdef __cplusplus\n" "cpp\n" @@ -2510,6 +2522,20 @@ class TestPreprocessor : public TestFixture { "if ( l > $2147483647 ) { }\n" "}", actual); } + + void hashCalculation() { + // #12383 + const char code[] = "int a;"; + const char code2[] = "int a;"; // extra space + const char code3[] = "\n\nint a;"; // extra new line + + ASSERT_EQUALS(getHash(code), getHash(code)); + ASSERT_EQUALS(getHash(code2), getHash(code2)); + ASSERT_EQUALS(getHash(code3), getHash(code3)); + ASSERT(getHash(code) != getHash(code2)); + ASSERT(getHash(code) != getHash(code3)); + ASSERT(getHash(code2) != getHash(code3)); + } }; REGISTER_TEST(TestPreprocessor)