jarin created this revision.
jarin added a reviewer: teemperor.
jarin added a project: LLDB.
Herald added subscribers: lldb-commits, JDevlieghere.
Herald added a reviewer: shafik.
jarin requested review of this revision.
This patch is for discussion.
Currently, the evaluator does not know about static const fields if
the code does not contain a definition (but only a declaration).
For example:
struct C { static const int f = 42; };
int main() {
return C::f; // Evaluating C::f does not work here!
}
This patch tries to fix that by initializing the varDecl of the static
field with the constant value. This works reasonably well when the
static member is only used as rvalue, but any lvalue usage still does
not work because the static member does not have a memory location.
To fix this properly, do we have to teach the materializer about the
static fields, too? What would be the best way to go about that?
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D85719
Files:
lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
lldb/test/API/lang/cpp/static_const_members/Makefile
lldb/test/API/lang/cpp/static_const_members/TestStaticConstMembers.py
lldb/test/API/lang/cpp/static_const_members/main.cpp
Index: lldb/test/API/lang/cpp/static_const_members/main.cpp
===
--- /dev/null
+++ lldb/test/API/lang/cpp/static_const_members/main.cpp
@@ -0,0 +1,29 @@
+enum class E {
+ E0 = 0,
+ E42 = 42,
+};
+
+struct C {
+ static const int static_const_i32 = 40;
+ static const int static_const_i32_neg = -40;
+ static constexpr int static_constexpr_i32 = 41;
+ static const unsigned static_const_ui32 = 0x;
+ static constexpr unsigned static_constexpr_ui32 = 0xfffe;
+ static const int static_const_var_i32;
+ static constexpr int static_constexpr_i32_other = 43;
+ static const E static_const_enum = E::E42;
+ static const char static_const_i8 = -1;
+};
+
+const int C::static_const_var_i32 = 42;
+constexpr int C::static_constexpr_i32_other;
+
+int deref(const int *p) { return *p; }
+
+int main() {
+ const int *pi = ::static_constexpr_i32_other;
+ int rv = C::static_const_i32 + C::static_const_i32_neg +
+ C::static_constexpr_i32 + (int)C::static_const_ui32 +
+ C::static_const_var_i32 + deref(pi);
+ return rv; // break here
+}
Index: lldb/test/API/lang/cpp/static_const_members/TestStaticConstMembers.py
===
--- /dev/null
+++ lldb/test/API/lang/cpp/static_const_members/TestStaticConstMembers.py
@@ -0,0 +1,50 @@
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+import lldbsuite.test.lldbutil as lldbutil
+
+
+class TestStaticConstMembers(TestBase):
+
+mydir = TestBase.compute_mydir(__file__)
+NO_DEBUG_INFO_TESTCASE = True
+
+def test_static_const_var_definition(self):
+self.build()
+lldbutil.run_to_source_breakpoint(self, "// break here",
+lldb.SBFileSpec("main.cpp", False))
+self.expect_expr("C::static_const_var_i32", result_value="42")
+self.expect_expr("C::static_constexpr_i32_other", result_value="43")
+
+@expectedFailureAll()
+def test_static_const_member_lval(self):
+self.build()
+lldbutil.run_to_source_breakpoint(self, "// break here",
+lldb.SBFileSpec("main.cpp", False))
+self.expect_expr("C::static_const_i32", result_value="40")
+self.expect_expr("C::static_const_i32_neg", result_value="-40")
+self.expect_expr("C::static_constexpr_i32", result_value="41")
+self.expect_expr("C::static_const_ui32", result_value="4294967295")
+self.expect_expr("C::static_const_enum", result_value="E42")
+self.expect_expr("C::static_const_i8", result_value="-1")
+
+def test_static_const_member_rval(self):
+self.build()
+lldbutil.run_to_source_breakpoint(self, "// break here",
+lldb.SBFileSpec("main.cpp", False))
+self.expect_expr("C::static_const_i32 + 2", result_value="42")
+self.expect_expr("-C::static_const_i32_neg", result_value="40")
+self.expect_expr("C::static_constexpr_i32 | 0", result_value="41")
+self.expect_expr("-C::static_const_ui32", result_value="1")
+self.expect_expr("C::static_const_ui32 + 0u", result_value="4294967295")
+self.expect_expr("-C::static_constexpr_ui32", result_value="2")
+self.expect_expr("(int)C::static_const_enum", result_value="42")
+self.expect_expr("C::static_const_i8 + 0", result_value="-1")
+
+@expectedFailureAll()
+def test_static_const_member_address(self):
+self.build()
+lldbutil.run_to_source_breakpoint(self, "// break here",
+lldb.SBFileSpec("main.cpp", False))
+self.expect_expr("deref(&(C::static_const_var_i32))", result_value="42")
+