[llvm] d64a22a - [LiveDebugValues] Prevent some misuse of LocIndex::fromRawInteger, NFC

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 16:59:17 PST 2020


Author: Vedant Kumar
Date: 2020-03-02T16:59:09-08:00
New Revision: d64a22a2add97ff1ae28acb18c9f6ea76a26ecf3

URL: https://github.com/llvm/llvm-project/commit/d64a22a2add97ff1ae28acb18c9f6ea76a26ecf3
DIFF: https://github.com/llvm/llvm-project/commit/d64a22a2add97ff1ae28acb18c9f6ea76a26ecf3.diff

LOG: [LiveDebugValues] Prevent some misuse of LocIndex::fromRawInteger, NFC

Make it a compile-time error to pass an int/unsigned/etc to
fromRawInteger.

Hopefully this prevents errors of the form:

```
for (unsigned ID : getVarLocs()) {
  auto VL = LocMap[LocIndex::fromRawInteger(ID)];
  ...
```

Added: 
    

Modified: 
    llvm/lib/CodeGen/LiveDebugValues.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/LiveDebugValues.cpp b/llvm/lib/CodeGen/LiveDebugValues.cpp
index ad54378d9edc..4d0c2462b7d3 100644
--- a/llvm/lib/CodeGen/LiveDebugValues.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues.cpp
@@ -138,7 +138,10 @@ struct LocIndex {
     return (static_cast<uint64_t>(Location) << 32) | Index;
   }
 
-  static LocIndex fromRawInteger(uint64_t ID) {
+  template<typename IntT> static LocIndex fromRawInteger(IntT ID) {
+    static_assert(std::is_unsigned<IntT>::value &&
+                      sizeof(ID) == sizeof(uint64_t),
+                  "Cannot convert raw integer to LocIndex");
     return {static_cast<uint32_t>(ID >> 32), static_cast<uint32_t>(ID)};
   }
 


        


More information about the llvm-commits mailing list