[llvm] [LLVM][ADT] Convert llvm::hash_code to unsigned explicitly in DenseMapInfo (PR #77743)

Andrei Golubev via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 11 01:57:51 PST 2024


https://github.com/andrey-golubev created https://github.com/llvm/llvm-project/pull/77743

The getHashValue() signature returns a value of type 'unsigned' while the hash_code could only be implicitly converted to 'size_t'. Depending on the C++ implementation, this may or may not be a narrowing conversion.

On some platform/compiler combination, this becomes a warning. To avoid the warning (and better highlight the narrowing), do an explicit conversion instead.

>From 9e03cc161d037b33fc5bdc7bfec4a7f7bb78d7b2 Mon Sep 17 00:00:00 2001
From: "Golubev, Andrey" <andrey.golubev at intel.com>
Date: Fri, 29 Dec 2023 15:31:53 +0000
Subject: [PATCH] [LLVM][ADT] Convert llvm::hash_code to unsigned explicitly in
 DenseMapInfo

The getHashValue() signature returns a value of type 'unsigned' while
the hash_code could only be implicitly converted to 'size_t'. Depending
on the C++ implementation, this may or may not be a narrowing conversion.

On some platform/compiler combination, this becomes a warning. To
avoid the warning (and better highlight the narrowing), do an explicit
conversion instead.

Co-authored-by: Orest Chura <orest.chura at intel.com>
---
 llvm/include/llvm/ADT/Hashing.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h
index 4e82ba9a6fba2a..a5477362a50793 100644
--- a/llvm/include/llvm/ADT/Hashing.h
+++ b/llvm/include/llvm/ADT/Hashing.h
@@ -677,7 +677,9 @@ template <typename T> hash_code hash_value(const std::optional<T> &arg) {
 template <> struct DenseMapInfo<hash_code, void> {
   static inline hash_code getEmptyKey() { return hash_code(-1); }
   static inline hash_code getTombstoneKey() { return hash_code(-2); }
-  static unsigned getHashValue(hash_code val) { return val; }
+  static unsigned getHashValue(hash_code val) {
+    return static_cast<unsigned>(size_t(val));
+  }
   static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
 };
 



More information about the llvm-commits mailing list