[Lldb-commits] [PATCH] D102685: [lldb] Encode `bool` as unsigned int

Andy Yankovsky via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue May 18 05:44:33 PDT 2021


werat created this revision.
werat added a reviewer: teemperor.
werat requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

`bool` is considered to be unsigned according to `std::is_unsigned<bool>::value` (and `Type::GetTypeInfo`). Encoding it as signed int works fine for normal variables and fields, but breaks when reading the values of boolean bitfields. If the field is declared as `bool b : 1` and has a value of `0b1`, the call to `SBValue::GetValueAsSigned()` will return `-1`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102685

Files:
  lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
  lldb/test/API/lang/cpp/bitfields/TestCppBitfields.py
  lldb/test/API/lang/cpp/bitfields/main.cpp


Index: lldb/test/API/lang/cpp/bitfields/main.cpp
===================================================================
--- lldb/test/API/lang/cpp/bitfields/main.cpp
+++ lldb/test/API/lang/cpp/bitfields/main.cpp
@@ -104,5 +104,17 @@
   uwbf.x = 0xFFFFFFFF;
   uwubf.x = 0xFFFFFFFF;
 
+  struct BoolBits {
+    bool a : 1;
+    bool b : 1;
+    bool c : 2;
+    bool d : 2;
+  } bb;
+
+  bb.a = 0b1;
+  bb.b = 0b0;
+  bb.c = 0b11;
+  bb.d = 0b01;
+
   return 0; // Set break point at this line.
 }
Index: lldb/test/API/lang/cpp/bitfields/TestCppBitfields.py
===================================================================
--- lldb/test/API/lang/cpp/bitfields/TestCppBitfields.py
+++ lldb/test/API/lang/cpp/bitfields/TestCppBitfields.py
@@ -16,9 +16,6 @@
         # Find the line number to break inside main().
         self.line = line_number('main.cpp', '// Set break point at this line.')
 
-    # BitFields exhibit crashes in record layout on Windows
-    # (http://llvm.org/pr21800)
-    @skipIfWindows
     def test_and_run_command(self):
         """Test 'frame variable ...' on a variable with bitfields."""
         self.build()
@@ -120,3 +117,30 @@
                 '(uint32_t) b_a = 2',
                 '(uint32_t:1) d_a = 1',
                 ])
+
+        self.expect(
+            "frame variable --show-types bb",
+            VARIABLES_DISPLAYED_CORRECTLY,
+            substrs=[
+                '(bool:1) a = true',
+                '(bool:1) b = false',
+                '(bool:2) c = true',
+                '(bool:2) d = true',
+                ])
+
+        bb = self.frame().FindVariable('bb')
+        bb_a = bb.GetChildAtIndex(0)
+        self.assertEqual(bb_a.GetValueAsUnsigned(), 1)
+        self.assertEqual(bb_a.GetValueAsSigned(), 1)
+
+        bb_b = bb.GetChildAtIndex(1)
+        self.assertEqual(bb_b.GetValueAsUnsigned(), 0)
+        self.assertEqual(bb_b.GetValueAsSigned(), 0)
+
+        bb_c = bb.GetChildAtIndex(2)
+        self.assertEqual(bb_c.GetValueAsUnsigned(), 1)
+        self.assertEqual(bb_c.GetValueAsSigned(), 1)
+
+        bb_d = bb.GetChildAtIndex(3)
+        self.assertEqual(bb_d.GetValueAsUnsigned(), 1)
+        self.assertEqual(bb_d.GetValueAsSigned(), 1)
Index: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
===================================================================
--- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -4698,7 +4698,6 @@
     case clang::BuiltinType::Void:
       break;
 
-    case clang::BuiltinType::Bool:
     case clang::BuiltinType::Char_S:
     case clang::BuiltinType::SChar:
     case clang::BuiltinType::WChar_S:
@@ -4709,6 +4708,7 @@
     case clang::BuiltinType::Int128:
       return lldb::eEncodingSint;
 
+    case clang::BuiltinType::Bool:
     case clang::BuiltinType::Char_U:
     case clang::BuiltinType::UChar:
     case clang::BuiltinType::WChar_U:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102685.346152.patch
Type: text/x-patch
Size: 2940 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210518/20b79a90/attachment.bin>


More information about the lldb-commits mailing list