Add a data formatter for std::function in libcxx
authorEnrico Granata <egranata@apple.com>
Wed, 31 Aug 2016 21:46:37 +0000 (21:46 +0000)
committerEnrico Granata <egranata@apple.com>
Wed, 31 Aug 2016 21:46:37 +0000 (21:46 +0000)
llvm-svn: 280295

lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/Makefile [new file with mode: 0644]
lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/TestLibCxxFunction.py [new file with mode: 0644]
lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/main.cpp [new file with mode: 0644]
lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp
lldb/source/Plugins/Language/CPlusPlus/LibCxx.h

diff --git a/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/Makefile b/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/Makefile
new file mode 100644 (file)
index 0000000..fdd7171
--- /dev/null
@@ -0,0 +1,5 @@
+LEVEL = ../../../../../make
+CXX_SOURCES := main.cpp
+CXXFLAGS += -std=c++11
+USE_LIBCPP := 1
+include $(LEVEL)/Makefile.rules
diff --git a/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/TestLibCxxFunction.py b/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/TestLibCxxFunction.py
new file mode 100644 (file)
index 0000000..a5eec8a
--- /dev/null
@@ -0,0 +1,50 @@
+"""
+Test lldb data formatter subsystem.
+"""
+
+from __future__ import print_function
+
+
+
+import os, time
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class LibCxxFunctionTestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def get_variable(self, name):
+        var = self.frame().FindVariable(name)
+        var.SetPreferDynamicValue(lldb.eDynamicCanRunTarget)
+        var.SetPreferSyntheticValue(True)
+        return var
+
+    @skipIf(compiler="gcc")
+    @skipIfWindows # libc++ not ported to Windows yet
+    def test(self):
+        """Test that std::function as defined by libc++ is correctly printed by LLDB"""
+        self.build()
+        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
+
+        bkpt = self.target().FindBreakpointByID(lldbutil.run_break_set_by_source_regexp (self, "Set break point at this line."))
+
+        self.runCmd("run", RUN_SUCCEEDED)
+
+        lldbutil.skip_if_library_missing(self, self.target(), lldbutil.PrintableRegex("libc\+\+"))
+
+        # The stop reason of the thread should be breakpoint.
+        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
+            substrs = ['stopped',
+                       'stop reason = breakpoint'])
+        
+        f1 = self.get_variable('f1')
+        f2 = self.get_variable('f2')
+        
+        if self.TraceOn(): print(f1)
+        if self.TraceOn(): print(f2)
+        
+        self.assertTrue(f1.GetValueAsUnsigned(0) != 0, 'f1 has a valid value')
+        self.assertTrue(f2.GetValueAsUnsigned(0) != 0, 'f2 has a valid value')
diff --git a/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/main.cpp b/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-stl/libcxx/function/main.cpp
new file mode 100644 (file)
index 0000000..cfe689b
--- /dev/null
@@ -0,0 +1,25 @@
+//===-- main.cpp --------------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <functional>
+
+int foo(int x, int y) {
+  return x + y - 1;
+}
+
+int main ()
+{
+  int acc = 42;
+  std::function<int (int,int)> f1 = foo;
+  std::function<int (int)> f2 = [acc,f1] (int x) -> int {
+    return x+f1(acc,x);
+  };
+    return f1(acc,acc) + f2(acc); // Set break point at this line.
+}
+
index 0cd059e..71bf160 100644 (file)
@@ -566,6 +566,8 @@ LoadLibCxxFormatters (lldb::TypeCategoryImplSP cpp_category_sp)
     
     AddCXXSummary(cpp_category_sp, lldb_private::formatters::LibcxxContainerSummaryProvider, "libc++ std::vector<bool> summary provider", ConstString("std::__(ndk)?1::vector<bool, std::__(ndk)?1::allocator<bool> >"), stl_summary_flags);
     AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEndCreator, "std::map iterator synthetic children", ConstString("^std::__(ndk)?1::__map_iterator<.+>$"), stl_synth_flags, true);
+
+    AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibcxxFunctionFrontEndCreator, "std::function synthetic value provider", ConstString("^std::__1::function<.+>$"), stl_synth_flags, true);
 #endif
 }
 
index beb89b8..1567fff 100644 (file)
@@ -645,3 +645,26 @@ lldb_private::formatters::LibcxxStringSummaryProvider (ValueObject& valobj, Stre
     
     return true;
 }
+
+class LibcxxFunctionFrontEnd : public SyntheticValueProviderFrontEnd
+{
+public:
+    LibcxxFunctionFrontEnd (ValueObject &backend) :
+    SyntheticValueProviderFrontEnd(backend)
+    {}
+    
+    lldb::ValueObjectSP
+    GetSyntheticValue() override
+    {
+        static ConstString g___f_("__f_");
+        return m_backend.GetChildMemberWithName(g___f_, true);
+    }
+};
+
+SyntheticChildrenFrontEnd*
+lldb_private::formatters::LibcxxFunctionFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
+{
+    if (valobj_sp)
+        return new LibcxxFunctionFrontEnd(*valobj_sp);
+    return nullptr;
+}
index ae00bc0..cc92b81 100644 (file)
@@ -134,6 +134,8 @@ namespace lldb_private {
         SyntheticChildrenFrontEnd* LibcxxStdUnorderedMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP);
         
         SyntheticChildrenFrontEnd* LibcxxInitializerListSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP);
+        
+        SyntheticChildrenFrontEnd* LibcxxFunctionFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP);
 
     } // namespace formatters
 } // namespace lldb_private