Skip to content

Commit ea2c390

Browse files
authored
Fix #12383: cppcheck build dir: changed line numbers in source file (#6505)
1 parent 70b1f66 commit ea2c390

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Diff for: lib/preprocessor.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -952,13 +952,19 @@ std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, con
952952
{
953953
std::string hashData = toolinfo;
954954
for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
955-
if (!tok->comment)
955+
if (!tok->comment) {
956956
hashData += tok->str();
957+
hashData += static_cast<char>(tok->location.line);
958+
hashData += static_cast<char>(tok->location.col);
959+
}
957960
}
958961
for (std::map<std::string, simplecpp::TokenList *>::const_iterator it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
959962
for (const simplecpp::Token *tok = it->second->cfront(); tok; tok = tok->next) {
960-
if (!tok->comment)
963+
if (!tok->comment) {
961964
hashData += tok->str();
965+
hashData += static_cast<char>(tok->location.line);
966+
hashData += static_cast<char>(tok->location.col);
967+
}
962968
}
963969
}
964970
return (std::hash<std::string>{})(hashData);

Diff for: test/testpreprocessor.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ class TestPreprocessor : public TestFixture {
264264
TEST_CASE(testMissingIncludeCheckConfig);
265265

266266
TEST_CASE(limitsDefines);
267+
268+
TEST_CASE(hashCalculation);
267269
}
268270

269271
std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
@@ -284,6 +286,16 @@ class TestPreprocessor : public TestFixture {
284286
return ret;
285287
}
286288

289+
std::size_t getHash(const char filedata[]) {
290+
Settings settings;
291+
Preprocessor preprocessor(settings, *this);
292+
std::vector<std::string> files;
293+
std::istringstream istr(filedata);
294+
simplecpp::TokenList tokens(istr,files);
295+
tokens.removeComments();
296+
return preprocessor.calculateHash(tokens, "");
297+
}
298+
287299
void Bug2190219() {
288300
const char filedata[] = "#ifdef __cplusplus\n"
289301
"cpp\n"
@@ -2510,6 +2522,20 @@ class TestPreprocessor : public TestFixture {
25102522
"if ( l > $2147483647 ) { }\n"
25112523
"}", actual);
25122524
}
2525+
2526+
void hashCalculation() {
2527+
// #12383
2528+
const char code[] = "int a;";
2529+
const char code2[] = "int a;"; // extra space
2530+
const char code3[] = "\n\nint a;"; // extra new line
2531+
2532+
ASSERT_EQUALS(getHash(code), getHash(code));
2533+
ASSERT_EQUALS(getHash(code2), getHash(code2));
2534+
ASSERT_EQUALS(getHash(code3), getHash(code3));
2535+
ASSERT(getHash(code) != getHash(code2));
2536+
ASSERT(getHash(code) != getHash(code3));
2537+
ASSERT(getHash(code2) != getHash(code3));
2538+
}
25132539
};
25142540

25152541
REGISTER_TEST(TestPreprocessor)

0 commit comments

Comments
 (0)