@@ -49,6 +49,7 @@ class TestErrorLogger : public TestFixture {
4949 TEST_CASE (FileLocationSetFile2);
5050 TEST_CASE (ErrorMessageConstruct);
5151 TEST_CASE (ErrorMessageConstructLocations);
52+ TEST_CASE (ErrorMessageHashFallback);
5253 TEST_CASE (ErrorMessageVerbose);
5354 TEST_CASE (ErrorMessageVerboseLocations);
5455 TEST_CASE (ErrorMessageVerboseSymbol);
@@ -267,6 +268,77 @@ class TestErrorLogger : public TestFixture {
267268 ASSERT_EQUALS (" [foo.cpp:5] -> [bar.cpp:8]: (error) Programming error." , msg.toString (true , templateFormat, " " ));
268269 }
269270
271+ // unusedFunction/staticFunction/ctu* warnings carry no token information, so
272+ // ErrorMessage::calculateWarningHash() (which needs tokens) can't compute a
273+ // hash for them. calculateWarningHashFromLocations() is the fallback used
274+ // instead - it hashes the id, message and all location filenames/notes.
275+ void ErrorMessageHashFallback () const {
276+ // ids that don't need a fallback hash still get none
277+ {
278+ std::list<ErrorMessage::FileLocation> locs (1 , fooCpp5);
279+ ErrorMessage msg (std::move (locs), " " , Severity::style, " Some warning." , " someOtherId" , Certainty::normal);
280+ ASSERT_EQUALS (0 , msg.hash );
281+ }
282+
283+ // unusedFunction, staticFunction and any ctu* id get a non-zero fallback hash
284+ for (const std::string& id : { std::string (" unusedFunction" ), std::string (" staticFunction" ), std::string (" ctuOneDefinitionRuleViolation" ) }) {
285+ std::list<ErrorMessage::FileLocation> locs (1 , fooCpp5);
286+ ErrorMessage msg (std::move (locs), " " , Severity::style, " Some warning." , id, Certainty::normal);
287+ ASSERT (msg.hash != 0 );
288+ }
289+
290+ // same id/message/locations => same hash
291+ {
292+ std::list<ErrorMessage::FileLocation> locs1 (1 , fooCpp5);
293+ ErrorMessage msg1 (std::move (locs1), " " , Severity::style, " Some warning." , " unusedFunction" , Certainty::normal);
294+
295+ std::list<ErrorMessage::FileLocation> locs2 (1 , fooCpp5);
296+ ErrorMessage msg2 (std::move (locs2), " " , Severity::style, " Some warning." , " unusedFunction" , Certainty::normal);
297+
298+ ASSERT_EQUALS (msg1.hash , msg2.hash );
299+ }
300+
301+ // different message => different hash
302+ {
303+ std::list<ErrorMessage::FileLocation> locs1 (1 , fooCpp5);
304+ ErrorMessage msg1 (std::move (locs1), " " , Severity::style, " Some warning." , " unusedFunction" , Certainty::normal);
305+
306+ std::list<ErrorMessage::FileLocation> locs2 (1 , fooCpp5);
307+ ErrorMessage msg2 (std::move (locs2), " " , Severity::style, " Some other warning." , " unusedFunction" , Certainty::normal);
308+
309+ ASSERT (msg1.hash != msg2.hash );
310+ }
311+
312+ // different location filename => different hash
313+ {
314+ std::list<ErrorMessage::FileLocation> locs1 (1 , fooCpp5);
315+ ErrorMessage msg1 (std::move (locs1), " " , Severity::style, " Some warning." , " unusedFunction" , Certainty::normal);
316+
317+ std::list<ErrorMessage::FileLocation> locs2 (1 , barCpp8);
318+ ErrorMessage msg2 (std::move (locs2), " " , Severity::style, " Some warning." , " unusedFunction" , Certainty::normal);
319+
320+ ASSERT (msg1.hash != msg2.hash );
321+ }
322+
323+ // different location note (info) => different hash
324+ {
325+ std::list<ErrorMessage::FileLocation> locs1 (1 , barCpp8);
326+ ErrorMessage msg1 (std::move (locs1), " " , Severity::error, " Some warning." , " ctuOneDefinitionRuleViolation" , Certainty::normal);
327+
328+ std::list<ErrorMessage::FileLocation> locs2 (1 , barCpp8_i);
329+ ErrorMessage msg2 (std::move (locs2), " " , Severity::error, " Some warning." , " ctuOneDefinitionRuleViolation" , Certainty::normal);
330+
331+ ASSERT (msg1.hash != msg2.hash );
332+ }
333+
334+ // hash shows up in the XML output
335+ {
336+ std::list<ErrorMessage::FileLocation> locs (1 , fooCpp5);
337+ ErrorMessage msg (std::move (locs), " " , Severity::style, " Some warning." , " unusedFunction" , Certainty::normal);
338+ ASSERT (msg.toXML ().find (" hash=\" " ) != std::string::npos);
339+ }
340+ }
341+
270342 void ErrorMessageVerbose () const {
271343 std::list<ErrorMessage::FileLocation> locs (1 , fooCpp5);
272344 ErrorMessage msg (std::move (locs), " " , Severity::error, " Programming error.\n Verbose error" , " errorId" , Certainty::normal);
0 commit comments