From ea2c39012ed09cd4297e42ab38a19d93475b1358 Mon Sep 17 00:00:00 2001 From: olabetskyi <153490942+olabetskyi@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:15:14 +0300 Subject: [PATCH] Fix #12383: cppcheck build dir: changed line numbers in source file (#6505) --- lib/preprocessor.cpp | 10 ++++++++-- test/testpreprocessor.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 738862e42eb..06d01cd1406 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -952,13 +952,19 @@ 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 += 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) + if (!tok->comment) { hashData += tok->str(); + hashData += static_cast(tok->location.line); + hashData += static_cast(tok->location.col); + } } } return (std::hash{})(hashData); 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)