[Lldb-commits] [lldb] a6926d5 - [lldb/API] Add SBCompileUnit::GetIndexForLineEntry method to SB API

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Thu May 12 16:55:23 PDT 2022


Author: Med Ismail Bennani
Date: 2022-05-12T16:54:22-07:00
New Revision: a6926d576131c9ad849fef6f1d43134caab5025e

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

LOG: [lldb/API] Add SBCompileUnit::GetIndexForLineEntry method to SB API

This patch adds a new `GetIndexForLineEntry` method to the `SBCompileUnit`
class. As the name suggests, given an `SBLineEntry` object, this will
return the line entry index within a specific compile unit.

This method can take a `exact` boolean that will make sure that the
provided line entry matches perfectly another line entry in the compile unit.

rdar://47450887

Differention Revision: https://reviews.llvm.org/D125437

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    lldb/test/API/python_api/compile_unit/Makefile
    lldb/test/API/python_api/compile_unit/TestCompileUnitAPI.py
    lldb/test/API/python_api/compile_unit/main.c

Modified: 
    lldb/bindings/interface/SBCompileUnit.i
    lldb/include/lldb/API/SBCompileUnit.h
    lldb/source/API/SBCompileUnit.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBCompileUnit.i b/lldb/bindings/interface/SBCompileUnit.i
index 4c8efaedb7dc9..a633ee78d6f7e 100644
--- a/lldb/bindings/interface/SBCompileUnit.i
+++ b/lldb/bindings/interface/SBCompileUnit.i
@@ -67,6 +67,22 @@ public:
     lldb::SBLineEntry
     GetLineEntryAtIndex (uint32_t idx) const;
 
+    %feature("docstring", "
+     Get the index for a provided line entry in this compile unit.
+
+     @param[in] line_entry
+        The SBLineEntry object for which we are looking for the index.
+
+     @param[in] exact
+        An optional boolean defaulting to false that ensures that the provided
+        line entry has a perfect match in the compile unit.
+
+     @return
+        The index of the user-provided line entry. UINT32_MAX if the line entry
+        was not found in the compile unit.") GetIndexForLineEntry;
+    uint32_t
+    GetIndexForLineEntry (lldb::SBLineEntry &line_entry, bool exact = false) const;
+
     uint32_t
     FindLineEntryIndex (uint32_t start_idx,
                         uint32_t line,

diff  --git a/lldb/include/lldb/API/SBCompileUnit.h b/lldb/include/lldb/API/SBCompileUnit.h
index d965b9d966b9f..18da48bf11711 100644
--- a/lldb/include/lldb/API/SBCompileUnit.h
+++ b/lldb/include/lldb/API/SBCompileUnit.h
@@ -34,6 +34,9 @@ class LLDB_API SBCompileUnit {
 
   lldb::SBLineEntry GetLineEntryAtIndex(uint32_t idx) const;
 
+  uint32_t GetIndexForLineEntry(lldb::SBLineEntry &line_entry,
+                                bool exact = false) const;
+
   uint32_t FindLineEntryIndex(uint32_t start_idx, uint32_t line,
                               lldb::SBFileSpec *inline_file_spec) const;
 

diff  --git a/lldb/source/API/SBCompileUnit.cpp b/lldb/source/API/SBCompileUnit.cpp
index 46a319c6b7a3a..610860f1be825 100644
--- a/lldb/source/API/SBCompileUnit.cpp
+++ b/lldb/source/API/SBCompileUnit.cpp
@@ -77,6 +77,20 @@ SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
   return sb_line_entry;
 }
 
+uint32_t SBCompileUnit::GetIndexForLineEntry(lldb::SBLineEntry &line_entry,
+                                             bool exact) const {
+  LLDB_INSTRUMENT_VA(this, line_entry, exact);
+
+  if (!m_opaque_ptr || !line_entry.IsValid())
+    return UINT32_MAX;
+
+  LineEntry found_line_entry;
+
+  return m_opaque_ptr->FindLineEntry(0, line_entry.GetLine(),
+                                     line_entry.GetFileSpec().get(), exact,
+                                     &line_entry.ref());
+}
+
 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
                                            SBFileSpec *inline_file_spec) const {
   LLDB_INSTRUMENT_VA(this, start_idx, line, inline_file_spec);

diff  --git a/lldb/test/API/python_api/compile_unit/Makefile b/lldb/test/API/python_api/compile_unit/Makefile
new file mode 100644
index 0000000000000..10495940055b6
--- /dev/null
+++ b/lldb/test/API/python_api/compile_unit/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules

diff  --git a/lldb/test/API/python_api/compile_unit/TestCompileUnitAPI.py b/lldb/test/API/python_api/compile_unit/TestCompileUnitAPI.py
new file mode 100644
index 0000000000000..2c4ebf7982d96
--- /dev/null
+++ b/lldb/test/API/python_api/compile_unit/TestCompileUnitAPI.py
@@ -0,0 +1,44 @@
+"""
+Test SBCompileUnit APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class CompileUnitAPITestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def setUp(self):
+        TestBase.setUp(self)
+
+    def test(self):
+        """Exercise some SBCompileUnit APIs."""
+        self.build()
+
+        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 'break here', lldb.SBFileSpec('main.c'))
+        self.assertTrue(target, VALID_TARGET)
+        self.assertTrue(process, PROCESS_IS_VALID)
+        self.assertTrue(bkpt and bkpt.GetNumLocations() == 1,
+                        VALID_BREAKPOINT)
+
+        self.assertTrue(
+            thread.IsValid(),
+            "There should be a thread stopped due to breakpoint condition")
+        frame0 = thread.GetFrameAtIndex(0)
+        line_entry = frame0.GetLineEntry()
+
+        sc_list = target.FindCompileUnits(line_entry.GetFileSpec())
+        self.assertGreater(sc_list.GetSize(), 0)
+
+        main_cu = sc_list.compile_units[0]
+        self.assertTrue(main_cu.IsValid(), "Main executable CU is not valid")
+
+        self.assertEqual(main_cu.GetIndexForLineEntry(line_entry, True),
+                         main_cu.FindLineEntryIndex(0, line_entry.GetLine(),
+                                   line_entry.GetFileSpec(), True))
+
+

diff  --git a/lldb/test/API/python_api/compile_unit/main.c b/lldb/test/API/python_api/compile_unit/main.c
new file mode 100644
index 0000000000000..fc53ac95d1789
--- /dev/null
+++ b/lldb/test/API/python_api/compile_unit/main.c
@@ -0,0 +1,25 @@
+int a(int);
+int b(int);
+int c(int);
+
+int a(int val) {
+  if (val <= 1)
+    val = b(val);
+  else if (val >= 3)
+    val = c(val);
+
+  return val;
+}
+
+int b(int val) { return c(val); }
+
+int c(int val) {
+  return val + 3; // break here.
+}
+
+int main(int argc, char const *argv[]) {
+  int A1 = a(1); // a(1) -> b(1) -> c(1)
+  int B2 = b(2); // b(2) -> c(2)
+  int A3 = a(3); // a(3) -> c(3)
+  return 0;
+}


        


More information about the lldb-commits mailing list