[Lldb-commits] [PATCH] D129327: [lldb] Add support for using integral const static data members in the expression evaluator

Andy Yankovsky via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 7 13:39:32 PDT 2022


werat created this revision.
Herald added a subscriber: mgorny.
Herald added a reviewer: shafik.
Herald added a project: All.
werat requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This adds support for using const static integral data members as described by C++11 [class.static.data]p3
to LLDB's expression evaluator.

So far LLDB treated these data members are normal static variables. They already work as intended when they are declared in the class definition and then defined in a namespace scope. However, if they are declared and initialised in the class definition but never defined in a namespace scope, all LLDB expressions that use them will fail to link when LLDB can't find the respective symbol for the variable.

The reason for this is that the data members which are only declared in the class are not emitted into any object file so LLDB can never resolve them. Expressions that use these variables are expected to directly use their constant value if possible. Clang can do this for us during codegen, but it requires that we add the constant value to the VarDecl we generate for these data members.

This patch implements this by:

- parsing the constant values from the debug info and adding it to variable declarations we encounter.
- ensuring that LLDB doesn't implicitly try to take the address of expressions that might be an lvalue that points to such a special data member.

The second change is caused by LLDB's way of storing lvalues in the expression parser. When LLDB parses an expression, it tries to keep the result around via two mechanisms:

1. For lvalues, LLDB generates a static pointer variable and stores the address of the last expression in it: `T *$__lldb_expr_result_ptr = &LastExpression`
2. For everything else, LLDB generates a static variable of the same type as the last expression and then direct initialises that variable: `T $__lldb_expr_result(LastExpression)`

If we try to print a special const static data member via something like `expr Class::Member`, then LLDB will try to take the address of this expression as it's an lvalue. This means LLDB will try to take the address of the variable which causes that Clang can't replace the use with the constant value. There isn't any good way to detect this case (as there a lot of different expressions that could yield an lvalue that points to such a data member), so this patch also changes that we only use the first way of capturing the result if the last expression does not have a type that could potentially indicate it's coming from such a special data member.

This change shouldn't break most workflows for users. The only observable side effect I could find is that the implicit persistent result variables for const int's now have their own memory address:

Before this change:

  (lldb) p i
  (const int) $0 = 123
  (lldb) p &$0
  (const int *) $1 = 0x00007ffeefbff8e8
  (lldb) p &i
  (const int *) $2 = 0x00007ffeefbff8e8

After this change we capture `i` by value so it has its own value.

  (lldb) p i
  (const int) $0 = 123
  (lldb) p &$0
  (const int *) $1 = 0x0000000100155320
  (lldb) p &i
  (const int *) $2 = 0x00007ffeefbff8e8

Co-authored-by: Raphael Isemann <teemperor at gmail.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129327

Files:
  lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
  lldb/test/API/lang/cpp/const_static_integral_member/Makefile
  lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py
  lldb/test/API/lang/cpp/const_static_integral_member/main.cpp
  lldb/test/API/lang/cpp/const_static_integral_member_int128/Makefile
  lldb/test/API/lang/cpp/const_static_integral_member_int128/TestConstStaticIntegralMemberInt128.py
  lldb/test/API/lang/cpp/const_static_integral_member_int128/main.cpp
  lldb/unittests/SymbolFile/DWARF/CMakeLists.txt
  lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129327.443050.patch
Type: text/x-patch
Size: 26838 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220707/9ae2a7c3/attachment-0001.bin>


More information about the lldb-commits mailing list