Skip to content

Commit

Permalink
Fix #12383: cppcheck build dir: changed line numbers in source file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
olabetskyi authored Jun 11, 2024
1 parent 70b1f66 commit ea2c390
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(tok->location.line);
hashData += static_cast<char>(tok->location.col);
}
}
for (std::map<std::string, simplecpp::TokenList *>::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<char>(tok->location.line);
hashData += static_cast<char>(tok->location.col);
}
}
}
return (std::hash<std::string>{})(hashData);
Expand Down
26 changes: 26 additions & 0 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<std::string> 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"
Expand Down Expand Up @@ -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)

0 comments on commit ea2c390

Please sign in to comment.