Skip to content

Commit 5729f59

Browse files
committed
account for ordering of options when computing FileSettings::hash
1 parent fc62be8 commit 5729f59

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/filesettings.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,25 @@ struct CPPCHECKLIB FileSettings {
9696

9797
hash ^= std::hash<std::string>{}(standard);
9898

99-
for (const auto &undef : undefs)
99+
for (const auto &undef : undefs) {
100+
hash = rotateLeft(hash, 1);
100101
hash ^= std::hash<std::string>{}(undef);
102+
}
101103

102-
for (const auto &includePath : includePaths)
104+
for (const auto &includePath : includePaths) {
105+
hash = rotateLeft(hash, 1);
103106
hash ^= std::hash<std::string>{}(includePath);
107+
}
104108

105-
for (const auto &systemIncludePath : systemIncludePaths)
109+
for (const auto &systemIncludePath : systemIncludePaths) {
110+
hash = rotateLeft(hash, 1);
106111
hash ^= std::hash<std::string>{}(systemIncludePath);
112+
}
107113

108-
for (const auto &define : splitString(defines, ';'))
114+
for (const auto &define : splitString(defines, ';')) {
115+
hash = rotateLeft(hash, 1);
109116
hash ^= std::hash<std::string>{}(define);
117+
}
110118
}
111119

112120
std::string cfg;

lib/utils.h

+6
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ static inline T* empty_if_null(T* p)
397397
return default_if_null(p, "");
398398
}
399399

400+
template<typename T>
401+
static inline T rotateLeft(T value, std::size_t amount)
402+
{
403+
return (value << amount) | (value >> (8 * sizeof(T) - amount));
404+
}
405+
400406
/**
401407
* Split string by given sperator.
402408
* @param str The string to split

test/testutils.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestUtils : public TestFixture {
4444
TEST_CASE(splitString);
4545
TEST_CASE(as_const);
4646
TEST_CASE(memoize);
47+
TEST_CASE(rotateLeft);
4748
}
4849

4950
void isValidGlobPattern() const {
@@ -535,6 +536,14 @@ class TestUtils : public TestFixture {
535536
ASSERT_EQUALS(1, callF());
536537
ASSERT_EQUALS(1, count);
537538
}
539+
540+
void rotateLeft() const {
541+
uint8_t value_u8 = 0b01010101;
542+
ASSERT_EQUALS(::rotateLeft(value_u8, 1), 0b10101010);
543+
544+
uint64_t value_u64 = 0xABCDEF0123456789;
545+
ASSERT_EQUALS(::rotateLeft(value_u64, 8), 0xCDEF0123456789AB);
546+
}
538547
};
539548

540549
REGISTER_TEST(TestUtils)

0 commit comments

Comments
 (0)