[lldb] Summary provider for char flexible array members

Add a summary provider which can print char[] members at the ends of
structs.

Differential Revision: https://reviews.llvm.org/D113174
This commit is contained in:
Pavel Labath 2021-10-26 20:37:29 +02:00
parent f0a670e93b
commit 35870c4422
5 changed files with 76 additions and 9 deletions

View File

@ -729,12 +729,8 @@ void FormatManager::LoadSystemFormatters() {
TypeCategoryImpl::SharedPointer sys_category_sp =
GetCategory(m_system_category_name);
sys_category_sp->GetTypeSummariesContainer()->Add(ConstString("char *"),
string_format);
sys_category_sp->GetTypeSummariesContainer()->Add(
ConstString("unsigned char *"), string_format);
sys_category_sp->GetTypeSummariesContainer()->Add(
ConstString("signed char *"), string_format);
sys_category_sp->GetRegexTypeSummariesContainer()->Add(
RegularExpression(R"(^((un)?signed )?char ?(\*|\[\])$)"), string_format);
sys_category_sp->GetRegexTypeSummariesContainer()->Add(
std::move(any_size_char_arr), string_array_format);

View File

@ -25,8 +25,8 @@ class TypeSummaryListArgumentTestCase(TestBase):
self.expect(
'type summary list char',
substrs=[
'char *',
'unsigned char'])
'char ?(\*|\[\])',
'char ?\[[0-9]+\]'])
self.expect(
'type summary list -w default',
@ -40,5 +40,7 @@ class TypeSummaryListArgumentTestCase(TestBase):
matching=False)
self.expect(
'type summary list -w system char',
substrs=['unsigned char *'],
substrs=[
'char ?(\*|\[\])',
'char ?\[[0-9]+\]'],
matching=True)

View File

@ -0,0 +1,3 @@
C_SOURCES := main.c
include Makefile.rules

View File

@ -0,0 +1,29 @@
"""
Tests C99's flexible array members.
"""
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class TestCase(TestBase):
mydir = TestBase.compute_mydir(__file__)
@no_debug_info_test
def test(self):
self.build()
lldbutil.run_to_source_breakpoint(self, "// break here",
lldb.SBFileSpec("main.c"))
self.expect_var_path("c->flexible", type="char[]", summary='"contents"')
self.expect_var_path("sc->flexible", type="signed char[]", summary='"contents"')
self.expect_var_path("uc->flexible", type="unsigned char[]", summary='"contents"')
# TODO: Make this work
self.expect("expr c->flexible", error=True,
substrs=["incomplete", "char[]"])
self.expect("expr sc->flexible", error=True,
substrs=["incomplete", "signed char[]"])
self.expect("expr uc->flexible", error=True,
substrs=["incomplete", "unsigned char[]"])

View File

@ -0,0 +1,37 @@
#include <stdlib.h>
#include <string.h>
struct WithFlexChar {
int member;
char flexible[];
};
struct WithFlexSChar {
int member;
signed char flexible[];
};
struct WithFlexUChar {
int member;
unsigned char flexible[];
};
#define CONTENTS "contents"
int main() {
struct WithFlexChar *c =
(struct WithFlexChar *)malloc(sizeof(int) + sizeof(CONTENTS));
c->member = 1;
strcpy(c->flexible, CONTENTS);
struct WithFlexSChar *sc =
(struct WithFlexSChar *)malloc(sizeof(int) + sizeof(CONTENTS));
sc->member = 1;
strcpy((char *)sc->flexible, CONTENTS);
struct WithFlexUChar *uc =
(struct WithFlexUChar *)malloc(sizeof(int) + sizeof(CONTENTS));
uc->member = 1;
strcpy((char *)uc->flexible, CONTENTS);
return 0; // break here
}