[llvm] r364230 - NFC: DataExtractor: use decodeULEB128 to implement getULEB128

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 13:43:36 PDT 2019


Author: dblaikie
Date: Mon Jun 24 13:43:36 2019
New Revision: 364230

URL: http://llvm.org/viewvc/llvm-project?rev=364230&view=rev
Log:
NFC: DataExtractor: use decodeULEB128 to implement getULEB128

Modified:
    llvm/trunk/lib/Support/DataExtractor.cpp

Modified: llvm/trunk/lib/Support/DataExtractor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/DataExtractor.cpp?rev=364230&r1=364229&r2=364230&view=diff
==============================================================================
--- llvm/trunk/lib/Support/DataExtractor.cpp (original)
+++ llvm/trunk/lib/Support/DataExtractor.cpp Mon Jun 24 13:43:36 2019
@@ -10,6 +10,7 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/SwapByteOrder.h"
+#include "llvm/Support/LEB128.h"
 using namespace llvm;
 
 template <typename T>
@@ -145,24 +146,17 @@ StringRef DataExtractor::getCStrRef(uint
 }
 
 uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
-  uint64_t result = 0;
-  if (Data.empty())
-    return 0;
-
-  unsigned shift = 0;
-  uint32_t offset = *offset_ptr;
-  uint8_t byte = 0;
+  assert(*offset_ptr <= Data.size());
 
-  while (isValidOffset(offset)) {
-    byte = Data[offset++];
-    result |= uint64_t(byte & 0x7f) << shift;
-    shift += 7;
-    if ((byte & 0x80) == 0) {
-      *offset_ptr = offset;
-      return result;
-    }
-  }
-  return 0;
+  const char *error;
+  unsigned bytes_read;
+  uint64_t result = decodeULEB128(
+      reinterpret_cast<const uint8_t *>(Data.data() + *offset_ptr), &bytes_read,
+      reinterpret_cast<const uint8_t *>(Data.data() + Data.size()), &error);
+  if (error)
+    return 0;
+  *offset_ptr += bytes_read;
+  return result;
 }
 
 int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {




More information about the llvm-commits mailing list