[Lldb-commits] [lldb] 025f6ca - [lldb] Modernize TestBitfields

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 25 04:12:44 PDT 2021


Author: Raphael Isemann
Date: 2021-10-25T13:11:39+02:00
New Revision: 025f6ca7c469b6412b58b69fab17092548d5f6c1

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

LOG: [lldb] Modernize TestBitfields

This just does the usual modernizations such as using new test functions where
possible, clang-formatting the source, avoiding manual process setup,
assert improvements (` assertTrue(a == b) -> assertEqual(a, b)`).

This doesn't add any new test cases but removes some dependence on unrelated
features where possible (e.g., structs declared in functions, using the standard
library to printf stuff or initialize objects).

Added: 
    

Modified: 
    lldb/test/API/lang/c/bitfields/TestBitfields.py
    lldb/test/API/lang/c/bitfields/main.c

Removed: 
    


################################################################################
diff  --git a/lldb/test/API/lang/c/bitfields/TestBitfields.py b/lldb/test/API/lang/c/bitfields/TestBitfields.py
index 2f52fce21ec57..4a144d5e80c65 100644
--- a/lldb/test/API/lang/c/bitfields/TestBitfields.py
+++ b/lldb/test/API/lang/c/bitfields/TestBitfields.py
@@ -1,4 +1,4 @@
-"""Show bitfields and check that they display correctly."""
+"""Test C bitfields."""
 
 
 
@@ -8,142 +8,100 @@
 from lldbsuite.test import lldbutil
 
 
-class BitfieldsTestCase(TestBase):
+class TestCase(TestBase):
 
     mydir = TestBase.compute_mydir(__file__)
 
-    def setUp(self):
-        # Call super's setUp().
-        TestBase.setUp(self)
-        # Find the line number to break inside main().
-        self.line = line_number('main.c', '// Set break point at this line.')
+    def run_to_main(self):
+        self.build()
+        lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.c"))
 
     # 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()
-        exe = self.getBuildArtifact("a.out")
-        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
-
-        # Break inside the main.
-        lldbutil.run_break_set_by_file_and_line(
-            self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
-
-        self.runCmd("run", RUN_SUCCEEDED)
-
-        # The stop reason of the thread should be breakpoint.
-        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
-                    substrs=['stopped',
-                             'stop reason = breakpoint'])
-
-        # The breakpoint should have a hit count of 1.
-        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
-                    substrs=[' resolved, hit count = 1'])
-
-        # This should display correctly.
-        self.expect(
-            "frame variable --show-types bits",
-            VARIABLES_DISPLAYED_CORRECTLY,
-            substrs=[
-                '(uint32_t:1) b1 = 1',
-                '(uint32_t:2) b2 = 3',
-                '(uint32_t:3) b3 = 7',
-                '(uint32_t) b4 = 15',
-                '(uint32_t:5) b5 = 31',
-                '(uint32_t:6) b6 = 63',
-                '(uint32_t:7) b7 = 127',
-                '(uint32_t:4) four = 15'])
-
-        # And so should this.
-        # rdar://problem/8348251
-        self.expect(
-            "frame variable --show-types",
-            VARIABLES_DISPLAYED_CORRECTLY,
-            substrs=[
-                '(uint32_t:1) b1 = 1',
-                '(uint32_t:2) b2 = 3',
-                '(uint32_t:3) b3 = 7',
-                '(uint32_t) b4 = 15',
-                '(uint32_t:5) b5 = 31',
-                '(uint32_t:6) b6 = 63',
-                '(uint32_t:7) b7 = 127',
-                '(uint32_t:4) four = 15'])
-
-        self.expect("expr (bits.b1)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '1'])
-        self.expect("expr (bits.b2)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '3'])
-        self.expect("expr (bits.b3)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '7'])
-        self.expect("expr (bits.b4)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '15'])
-        self.expect("expr (bits.b5)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '31'])
-        self.expect("expr (bits.b6)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '63'])
-        self.expect("expr (bits.b7)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '127'])
-        self.expect("expr (bits.four)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '15'])
-
-        self.expect(
-            "frame variable --show-types more_bits",
-            VARIABLES_DISPLAYED_CORRECTLY,
-            substrs=[
-                '(uint32_t:3) a = 3',
-                '(uint8_t:1) b = \'\\0\'',
-                '(uint8_t:1) c = \'\\x01\'',
-                '(uint8_t:1) d = \'\\0\''])
-
-        self.expect("expr (more_bits.a)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', '3'])
-        self.expect("expr (more_bits.b)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint8_t', '\\0'])
-        self.expect("expr (more_bits.c)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint8_t', '\\x01'])
-        self.expect("expr (more_bits.d)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint8_t', '\\0'])
-
-        self.expect("expr (packed.a)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['char', "'a'"])
-        self.expect("expr (packed.b)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', "10"])
-        self.expect("expr/x (packed.c)", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint32_t', "7112233"])
-
-        for bit in range(1,18):
-            expected = "1" if bit in [1, 5, 7, 13] else "0"
-            self.expect("expr even_more_bits.b" + str(bit), VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint8_t', expected])
-
-        for bit in [3, 10, 14]:
-            self.expect("expr even_more_bits.b" + str(bit) + " = 1", VARIABLES_DISPLAYED_CORRECTLY,
-                    substrs=['uint8_t', "1"])
-
-        self.expect(
-            "frame variable --show-types even_more_bits",
-            VARIABLES_DISPLAYED_CORRECTLY,
-            substrs=[
-                '(uint8_t:1) b1 = \'\\x01\'',
-                '(uint8_t:1) b2 = \'\\0\'',
-                '(uint8_t:1) b3 = \'\\x01\'',
-                '(uint8_t:1) b4 = \'\\0\'',
-                '(uint8_t:1) b5 = \'\\x01\'',
-                '(uint8_t:1) b6 = \'\\0\'',
-                '(uint8_t:1) b7 = \'\\x01\'',
-                '(uint8_t:1) b8 = \'\\0\'',
-                '(uint8_t:1) b9 = \'\\0\'',
-                '(uint8_t:1) b10 = \'\\x01\'',
-                '(uint8_t:1) b12 = \'\\0\'',
-                '(uint8_t:1) b13 = \'\\x01\'',
-                '(uint8_t:1) b14 = \'\\x01\'',
-                '(uint8_t:1) b15 = \'\\0\'',
-                '(uint8_t:1) b16 = \'\\0\'',
-                '(uint8_t:1) b17 = \'\\0\'',
-                ])
-
+    def test_bits(self):
+        self.run_to_main()
+
+        # Check each field of Bits.
+        bits_children = [
+            ValueCheck(type="int:1"), # Unnamed and uninitialized
+            ValueCheck(type="uint32_t:1", name="b1", value="1"),
+            ValueCheck(type="uint32_t:2", name="b2", value="3"),
+            ValueCheck(type="int:2"), # Unnamed and uninitialized
+            ValueCheck(type="uint32_t:3", name="b3", value="7"),
+            ValueCheck(type="uint32_t", name="b4", value="15"),
+            ValueCheck(type="uint32_t:5", name="b5", value="31"),
+            ValueCheck(type="uint32_t:6", name="b6", value="63"),
+            ValueCheck(type="uint32_t:7", name="b7", value="127"),
+            ValueCheck(type="uint32_t:4", name="four", value="15")
+        ]
+        self.expect_var_path("bits", type="Bits", children=bits_children)
+        self.expect_expr("bits", result_children=bits_children)
+
+        # Try accessing the 
diff erent fields using the expression evaluator.
+        self.expect_expr("bits.b1", result_type="uint32_t", result_value="1")
+        self.expect_expr("bits.b2", result_type="uint32_t", result_value="3")
+        self.expect_expr("bits.b3", result_type="uint32_t", result_value="7")
+        self.expect_expr("bits.b4", result_type="uint32_t", result_value="15")
+        self.expect_expr("bits.b5", result_type="uint32_t", result_value="31")
+        self.expect_expr("bits.b6", result_type="uint32_t", result_value="63")
+        self.expect_expr("bits.b7", result_type="uint32_t", result_value="127")
+        self.expect_expr("bits.four", result_type="uint32_t", result_value="15")
+
+        # Try accessing the 
diff erent fields using variable paths.
+        self.expect_var_path("bits.b1", type="uint32_t:1", value="1")
+        self.expect_var_path("bits.b2", type="uint32_t:2", value="3")
+        self.expect_var_path("bits.b4", type="uint32_t", value="15")
+        self.expect_var_path("bits.b5", type="uint32_t:5", value="31")
+        self.expect_var_path("bits.b7", type="uint32_t:7", value="127")
+
+
+        # Check each field of MoreBits.
+        more_bits_children = [
+            ValueCheck(type="uint32_t:3", name="a", value="3"),
+            ValueCheck(type="int:1", value="0"),
+            ValueCheck(type="uint8_t:1", name="b", value="'\\0'"),
+            ValueCheck(type="uint8_t:1", name="c", value="'\\x01'"),
+            ValueCheck(type="uint8_t:1", name="d", value="'\\0'"),
+        ]
+        self.expect_var_path("more_bits", type="MoreBits", children=more_bits_children)
+        self.expect_expr("more_bits", result_children=more_bits_children)
+
+        self.expect_expr("more_bits.a", result_type="uint32_t", result_value="3")
+        self.expect_expr("more_bits.b", result_type="uint8_t", result_value="'\\0'")
+        self.expect_expr("more_bits.c", result_type="uint8_t", result_value="'\\x01'")
+        self.expect_expr("more_bits.d", result_type="uint8_t", result_value="'\\0'")
+
+        # Test a struct with several single bit fields.
+        many_single_bits_children = [
+            ValueCheck(type="uint16_t:1", name="b1", value="1"),
+            ValueCheck(type="uint16_t:1", name="b2", value="0"),
+            ValueCheck(type="uint16_t:1", name="b3", value="0"),
+            ValueCheck(type="uint16_t:1", name="b4", value="0"),
+            ValueCheck(type="uint16_t:1", name="b5", value="1"),
+            ValueCheck(type="uint16_t:1", name="b6", value="0"),
+            ValueCheck(type="uint16_t:1", name="b7", value="1"),
+            ValueCheck(type="uint16_t:1", name="b8", value="0"),
+            ValueCheck(type="uint16_t:1", name="b9", value="0"),
+            ValueCheck(type="uint16_t:1", name="b10", value="0"),
+            ValueCheck(type="uint16_t:1", name="b11", value="0"),
+            ValueCheck(type="uint16_t:1", name="b12", value="0"),
+            ValueCheck(type="uint16_t:1", name="b13", value="1"),
+            ValueCheck(type="uint16_t:1", name="b14", value="0"),
+            ValueCheck(type="uint16_t:1", name="b15", value="0"),
+            ValueCheck(type="uint16_t:1", name="b16", value="0"),
+            ValueCheck(type="uint16_t:1", name="b17", value="0"),
+        ]
+        self.expect_var_path("many_single_bits", type="ManySingleBits", children=many_single_bits_children)
+        self.expect_expr("many_single_bits", result_type="ManySingleBits",
+            result_children=many_single_bits_children)
+
+        # Check a packed struct.
+        self.expect_expr("packed.a", result_type="char", result_value="'a'")
+        self.expect_expr("packed.b", result_type="uint32_t", result_value="10")
+        self.expect_expr("packed.c", result_type="uint32_t", result_value=str(int("7112233", 16)))
+
+        # A packed struct with bitfield size > 32.
         self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY,
                     substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"])
 
@@ -161,9 +119,8 @@ def test_expression_bug(self):
         # values for already parsed variables. The expression is run twice
         # because the very first expression can resume a target (to allocate
         # memory, etc.) even if it is not being jitted.
-        self.build()
-        lldbutil.run_to_line_breakpoint(self, lldb.SBFileSpec("main.c"),
-                self.line)
+        self.run_to_main()
+
         self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY,
                     substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"])
         self.expect("expr --allow-jit false  -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY,
@@ -181,67 +138,33 @@ def test_expression_bug(self):
     @skipIfWindows
     def test_and_python_api(self):
         """Use Python APIs to inspect a bitfields variable."""
-        self.build()
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
-
-        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
-        self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
-        process = target.LaunchSimple(
-            None, None, self.get_process_working_directory())
-        self.assertTrue(process, PROCESS_IS_VALID)
-
-        # The stop reason of the thread should be breakpoint.
-        thread = lldbutil.get_stopped_thread(
-            process, lldb.eStopReasonBreakpoint)
-        self.assertIsNotNone(thread)
-
-        # The breakpoint should have a hit count of 1.
-        self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE)
+        self.run_to_main()
 
         # Lookup the "bits" variable which contains 8 bitfields.
-        frame = thread.GetFrameAtIndex(0)
-        bits = frame.FindVariable("bits")
+        bits = self.frame().FindVariable("bits")
         self.DebugSBValue(bits)
-        self.assertEqual(
-            bits.GetTypeName(), 'Bits',
-            "bits.GetTypeName() == 'Bits'")
-        self.assertEqual(
-            bits.GetNumChildren(), 10,
-            "bits.GetNumChildren() == 10")
-        test_compiler = self.getCompiler()
-        self.assertEqual(bits.GetByteSize(), 32, "bits.GetByteSize() == 32")
+        self.assertEqual(bits.GetTypeName(), 'Bits')
+        self.assertEqual(bits.GetNumChildren(), 10)
+        self.assertEqual(bits.GetByteSize(), 32)
 
         # Notice the pattern of int(b1.GetValue(), 0).  We pass a base of 0
         # so that the proper radix is determined based on the contents of the
         # string.
         b1 = bits.GetChildMemberWithName("b1")
         self.DebugSBValue(b1)
-        self.assertTrue(b1.GetName() == "b1" and
-                        b1.GetTypeName() == "uint32_t:1" and
-                        b1.IsInScope() and
-                        int(b1.GetValue(), 0) == 1,
-                        'bits.b1 has type uint32_t:1, is in scope, and == 1')
+        self.assertEqual(b1.GetName(), "b1")
+        self.assertEqual(b1.GetTypeName(), "uint32_t:1")
+        self.assertTrue(b1.IsInScope())
+        self.assertEqual(int(b1.GetValue(), 0), 1)
 
         b7 = bits.GetChildMemberWithName("b7")
-        self.DebugSBValue(b7)
-        self.assertTrue(b7.GetName() == "b7" and
-                        b7.GetTypeName() == "uint32_t:7" and
-                        b7.IsInScope() and
-                        int(b7.GetValue(), 0) == 127,
-                        'bits.b7 has type uint32_t:7, is in scope, and == 127')
+        self.assertEqual(b7.GetName(), "b7")
+        self.assertEqual(b7.GetTypeName(), "uint32_t:7")
+        self.assertTrue(b7.IsInScope())
+        self.assertEqual(int(b7.GetValue(), 0), 127)
 
         four = bits.GetChildMemberWithName("four")
-        self.DebugSBValue(four)
-        self.assertTrue(four.GetName() == "four" and
-                        four.GetTypeName() == "uint32_t:4" and
-                        four.IsInScope() and
-                        int(four.GetValue(), 0) == 15,
-                        'bits.four has type uint32_t:4, is in scope, and == 15')
-
-        # Now kill the process, and we are done.
-        rc = target.GetProcess().Kill()
-        self.assertTrue(rc.Success())
+        self.assertEqual(four.GetName(), "four")
+        self.assertEqual(four.GetTypeName(), "uint32_t:4")
+        self.assertTrue(four.IsInScope())
+        self.assertEqual(int(four.GetValue(), 0), 15)
\ No newline at end of file

diff  --git a/lldb/test/API/lang/c/bitfields/main.c b/lldb/test/API/lang/c/bitfields/main.c
index f3f4d6a2c6f79..ba085f2aa94c2 100644
--- a/lldb/test/API/lang/c/bitfields/main.c
+++ b/lldb/test/API/lang/c/bitfields/main.c
@@ -1,97 +1,77 @@
 #include <stdint.h>
-#include <stdio.h>
-#include <string.h>
 
-int main (int argc, char const *argv[])
-{
-    struct Bits
-    {
-        uint32_t    : 1, // Unnamed bitfield
-                    b1 : 1,
-                    b2 : 2,
-                    : 2, // Unnamed bitfield
-                    b3 : 3,
-                    : 2, // Unnamed bitfield (this will get removed)
-                    b4 __attribute__ ((aligned(16))),
-                    b5 : 5,
-                    b6 : 6,
-                    b7 : 7,
-                    four : 4;
-    };
+struct Bits {
+  uint32_t    : 1;
+  uint32_t    b1 : 1;
+  uint32_t    b2 : 2;
+  uint32_t    : 2;
+  uint32_t    b3 : 3;
+  // Unnamed bitfield (this will get removed).
+  uint32_t    : 2;
+  uint32_t    b4 __attribute__ ((aligned(16)));
+  uint32_t    b5 : 5;
+  uint32_t    b6 : 6;
+  uint32_t    b7 : 7;
+  uint32_t    four : 4;
+};
 
-    printf("%lu", sizeof(struct Bits));
+struct MoreBits {
+  uint32_t a : 3;
+  uint8_t : 1;
+  uint8_t b : 1;
+  uint8_t c : 1;
+  uint8_t d : 1;
+};
+struct MoreBits more_bits;
 
-    struct Bits bits;
-    int i;
-    for (i=0; i<(1<<1); i++)
-        bits.b1 = i;        //// break $source:$line
-    for (i=0; i<(1<<2); i++)
-        bits.b2 = i;        //// break $source:$line
-    for (i=0; i<(1<<3); i++)
-        bits.b3 = i;        //// break $source:$line
-    for (i=0; i<(1<<4); i++)
-        bits.b4 = i;        //// break $source:$line
-    for (i=0; i<(1<<5); i++)
-        bits.b5 = i;        //// break $source:$line
-    for (i=0; i<(1<<6); i++)
-        bits.b6 = i;        //// break $source:$line
-    for (i=0; i<(1<<7); i++)
-        bits.b7 = i;        //// break $source:$line
-    for (i=0; i<(1<<4); i++)
-        bits.four = i;      //// break $source:$line
+struct ManySingleBits {
+  uint16_t b1 : 1, b2 : 1, b3 : 1, b4 : 1, b5 : 1, b6 : 1, b7 : 1, b8 : 1,
+      b9 : 1, b10 : 1, b11 : 1, b12 : 1, b13 : 1, b14 : 1, b15 : 1, b16 : 1,
+      b17 : 1;
+};
+struct ManySingleBits many_single_bits;
 
-    struct MoreBits
-    {
-        uint32_t    a : 3;
-        uint8_t       : 1;
-        uint8_t     b : 1;
-        uint8_t     c : 1;
-        uint8_t     d : 1;
-    };
+struct LargePackedBits {
+  uint64_t a : 36;
+  uint64_t b : 36;
+} __attribute__((packed));
 
-    struct MoreBits more_bits;
-
-    more_bits.a = 3;
-    more_bits.b = 0;
-    more_bits.c = 1;
-    more_bits.d = 0;
+#pragma pack(1)
+struct PackedBits {
+  char a;
+  uint32_t b : 5, c : 27;
+};
+#pragma pack()
 
-    struct EvenMoreBits
-    {
-        uint8_t b1  : 1, b2  : 1, b3  : 1, b4  : 1, b5  : 1, b6  : 1,
-                b7  : 1, b8  : 1, b9  : 1, b10 : 1, b11 : 1, b12 : 1,
-                b13 : 1, b14 : 1, b15 : 1, b16 : 1, b17 : 1;
-    };
+int main(int argc, char const *argv[]) {
+  struct Bits bits;
+  bits.b1 = 1;
+  bits.b2 = 3;
+  bits.b3 = 7;
+  bits.b4 = 15;
+  bits.b5 = 31;
+  bits.b6 = 63;
+  bits.b7 = 127;
+  bits.four = 15;
 
-    struct EvenMoreBits even_more_bits;
-    memset(&even_more_bits, 0, sizeof(even_more_bits));
-    even_more_bits.b1 = 1;
-    even_more_bits.b5 = 1;
-    even_more_bits.b7 = 1;
-    even_more_bits.b13 = 1;
+  more_bits.a = 3;
+  more_bits.b = 0;
+  more_bits.c = 1;
+  more_bits.d = 0;
 
-#pragma pack(1)
-    struct PackedBits
-    {
-        char a;
-    	uint32_t b : 5,
-                 c : 27;
-    };
-#pragma pack()  
-    struct PackedBits packed;
-    packed.a = 'a';
-    packed.b = 10;
-    packed.c = 0x7112233;
+  many_single_bits.b1 = 1;
+  many_single_bits.b5 = 1;
+  many_single_bits.b7 = 1;
+  many_single_bits.b13 = 1;
 
-    struct LargePackedBits {
-        uint64_t a: 36;
-        uint64_t b: 36;
-    } __attribute__((packed));
+  struct PackedBits packed;
+  packed.a = 'a';
+  packed.b = 10;
+  packed.c = 0x7112233;
 
-    struct LargePackedBits large_packed =
-      (struct LargePackedBits){ 0xcbbbbaaaa, 0xdffffeeee };
-    struct LargePackedBits *large_packed_ptr = &large_packed;
-    
-    return 0;               //// Set break point at this line.
+  struct LargePackedBits large_packed =
+      (struct LargePackedBits){0xcbbbbaaaa, 0xdffffeeee};
+  struct LargePackedBits *large_packed_ptr = &large_packed;
 
+  return 0; // break here
 }


        


More information about the lldb-commits mailing list