mib updated this revision to Diff 428841.
mib added a comment.

I forgot to add the test in the previous diffs --'


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125437/new/

https://reviews.llvm.org/D125437

Files:
  lldb/bindings/interface/SBCompileUnit.i
  lldb/include/lldb/API/SBCompileUnit.h
  lldb/source/API/SBCompileUnit.cpp
  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

Index: lldb/test/API/python_api/compile_unit/main.c
===================================================================
--- /dev/null
+++ 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;
+}
Index: lldb/test/API/python_api/compile_unit/TestCompileUnitAPI.py
===================================================================
--- /dev/null
+++ 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))
+
+
Index: lldb/test/API/python_api/compile_unit/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/python_api/compile_unit/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules
Index: lldb/source/API/SBCompileUnit.cpp
===================================================================
--- lldb/source/API/SBCompileUnit.cpp
+++ lldb/source/API/SBCompileUnit.cpp
@@ -77,6 +77,20 @@
   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);
Index: lldb/include/lldb/API/SBCompileUnit.h
===================================================================
--- lldb/include/lldb/API/SBCompileUnit.h
+++ lldb/include/lldb/API/SBCompileUnit.h
@@ -34,6 +34,9 @@
 
   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;
 
Index: lldb/bindings/interface/SBCompileUnit.i
===================================================================
--- lldb/bindings/interface/SBCompileUnit.i
+++ lldb/bindings/interface/SBCompileUnit.i
@@ -67,6 +67,22 @@
     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,
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to