[LiveDebugValues] Prevent some misuse of LocIndex::fromRawInteger, NFC
authorVedant Kumar <vsk@apple.com>
Tue, 3 Mar 2020 00:56:17 +0000 (16:56 -0800)
committerVedant Kumar <vsk@apple.com>
Tue, 3 Mar 2020 00:59:09 +0000 (16:59 -0800)
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)];
  ...
```

llvm/lib/CodeGen/LiveDebugValues.cpp

index ad54378..4d0c246 100644 (file)
@@ -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)};
   }