[Lldb-commits] [lldb] r369970 - [Core] GetAPInt should return an Optional

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 26 14:09:58 PDT 2019


Author: xiaobai
Date: Mon Aug 26 14:09:57 2019
New Revision: 369970

URL: http://llvm.org/viewvc/llvm-project?rev=369970&view=rev
Log:
[Core] GetAPInt should return an Optional

The current implementation returns a bool for indicating success and
whether or not the APInt passed by reference was populated. Instead of
doing that, I think it makes more sense to return an Optional<APInt>.

Modified:
    lldb/trunk/source/Core/DumpDataExtractor.cpp

Modified: lldb/trunk/source/Core/DumpDataExtractor.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/DumpDataExtractor.cpp?rev=369970&r1=369969&r2=369970&view=diff
==============================================================================
--- lldb/trunk/source/Core/DumpDataExtractor.cpp (original)
+++ lldb/trunk/source/Core/DumpDataExtractor.cpp Mon Aug 26 14:09:57 2019
@@ -20,6 +20,7 @@
 #include "lldb/Target/SectionLoadList.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/DataExtractor.h"
+#include "lldb/Utility/Log.h"
 #include "lldb/Utility/Stream.h"
 
 #include "clang/AST/ASTContext.h"
@@ -28,6 +29,7 @@
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 
 #include <limits>
@@ -64,8 +66,9 @@ static float half2float(uint16_t half) {
   return u.f * ldexpf(1, -112);
 }
 
-static bool GetAPInt(const DataExtractor &data, lldb::offset_t *offset_ptr,
-                     lldb::offset_t byte_size, llvm::APInt &result) {
+static llvm::Optional<llvm::APInt> GetAPInt(const DataExtractor &data,
+                                            lldb::offset_t *offset_ptr,
+                                            lldb::offset_t byte_size) {
   llvm::SmallVector<uint64_t, 2> uint64_array;
   lldb::offset_t bytes_left = byte_size;
   uint64_t u64;
@@ -81,8 +84,7 @@ static bool GetAPInt(const DataExtractor
       }
       uint64_array.push_back(u64);
     }
-    result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
-    return true;
+    return llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
   } else if (byte_order == lldb::eByteOrderBig) {
     lldb::offset_t be_offset = *offset_ptr + byte_size;
     lldb::offset_t temp_offset;
@@ -101,18 +103,17 @@ static bool GetAPInt(const DataExtractor
       uint64_array.push_back(u64);
     }
     *offset_ptr += byte_size;
-    result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
-    return true;
+    return llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
   }
-  return false;
+  return llvm::None;
 }
 
 static lldb::offset_t DumpAPInt(Stream *s, const DataExtractor &data,
                                 lldb::offset_t offset, lldb::offset_t byte_size,
                                 bool is_signed, unsigned radix) {
-  llvm::APInt apint;
-  if (GetAPInt(data, &offset, byte_size, apint)) {
-    std::string apint_str(apint.toString(radix, is_signed));
+  llvm::Optional<llvm::APInt> apint = GetAPInt(data, &offset, byte_size);
+  if (apint.hasValue()) {
+    std::string apint_str(apint.getValue().toString(radix, is_signed));
     switch (radix) {
     case 2:
       s->Write("0b", 2);
@@ -572,10 +573,11 @@ lldb::offset_t lldb_private::DumpDataExt
                                     apint);
               apfloat.toString(sv, format_precision, format_max_padding);
             } else if (item_bit_size == ast->getTypeSize(ast->DoubleTy)) {
-              llvm::APInt apint;
-              if (GetAPInt(DE, &offset, item_byte_size, apint)) {
+              llvm::Optional<llvm::APInt> apint =
+                  GetAPInt(DE, &offset, item_byte_size);
+              if (apint.hasValue()) {
                 llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->DoubleTy),
-                                      apint);
+                                      apint.getValue());
                 apfloat.toString(sv, format_precision, format_max_padding);
               }
             } else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy)) {
@@ -586,9 +588,10 @@ lldb::offset_t lldb_private::DumpDataExt
               if (&semantics == &llvm::APFloatBase::x87DoubleExtended())
                 byte_size = (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
 
-              llvm::APInt apint;
-              if (GetAPInt(DE, &offset, byte_size, apint)) {
-                llvm::APFloat apfloat(semantics, apint);
+              llvm::Optional<llvm::APInt> apint =
+                  GetAPInt(DE, &offset, byte_size);
+              if (apint.hasValue()) {
+                llvm::APFloat apfloat(semantics, apint.getValue());
                 apfloat.toString(sv, format_precision, format_max_padding);
               }
             } else if (item_bit_size == ast->getTypeSize(ast->HalfTy)) {




More information about the lldb-commits mailing list