libdrgn: add DRGN_FORMAT_OBJECT_{MEMBERS,ELEMENTS}_SAME_LINE

This commit is contained in:
Omar Sandoval 2019-12-09 12:52:05 -08:00
parent c3f69ba559
commit 4fad941ec1
6 changed files with 37 additions and 8 deletions

View File

@ -810,6 +810,14 @@ Objects
:param element_type_names: Include the type names of array elements.
Defaults to ``False``.
:type element_type_names: bool or None
:param members_same_line: Place multiple structure, union, and class
members on the same line if they fit within the specified
number of ``columns``. Defaults to ``False``.
:type members_same_line: bool or None
:param elements_same_line: Place multiple array elements on the same
line if they fit within the specified number of ``columns``.
Defaults to ``True``.
:type elements_same_line: bool or None
:rtype: str
.. function:: NULL(prog, type)

View File

@ -2009,13 +2009,16 @@ enum drgn_format_object_flags {
DRGN_FORMAT_OBJECT_TYPE_NAME = 1 << 4,
DRGN_FORMAT_OBJECT_MEMBER_TYPE_NAMES = 1 << 5,
DRGN_FORMAT_OBJECT_ELEMENT_TYPE_NAMES = 1 << 6,
DRGN_FORMAT_OBJECT_MEMBERS_SAME_LINE = 1 << 7,
DRGN_FORMAT_OBJECT_ELEMENTS_SAME_LINE = 1 << 8,
/** Default "pretty" flags. */
DRGN_FORMAT_OBJECT_PRETTY = (DRGN_FORMAT_OBJECT_DEREFERENCE |
DRGN_FORMAT_OBJECT_SYMBOLIZE |
DRGN_FORMAT_OBJECT_STRING |
DRGN_FORMAT_OBJECT_TYPE_NAME |
DRGN_FORMAT_OBJECT_MEMBER_TYPE_NAMES),
DRGN_FORMAT_OBJECT_VALID_FLAGS = (1 << 7) - 1,
DRGN_FORMAT_OBJECT_MEMBER_TYPE_NAMES |
DRGN_FORMAT_OBJECT_ELEMENTS_SAME_LINE),
DRGN_FORMAT_OBJECT_VALID_FLAGS = (1 << 9) - 1,
};
/**

View File

@ -130,7 +130,9 @@ drgn_passthrough_format_object_flags(enum drgn_format_object_flags flags)
DRGN_FORMAT_OBJECT_STRING |
DRGN_FORMAT_OBJECT_CHAR |
DRGN_FORMAT_OBJECT_MEMBER_TYPE_NAMES |
DRGN_FORMAT_OBJECT_ELEMENT_TYPE_NAMES));
DRGN_FORMAT_OBJECT_ELEMENT_TYPE_NAMES |
DRGN_FORMAT_OBJECT_MEMBERS_SAME_LINE |
DRGN_FORMAT_OBJECT_ELEMENTS_SAME_LINE));
}
/** Return flags that should be passed when formatting object members. */

View File

@ -1081,7 +1081,7 @@ compound_initializer_append_designation(struct initializer_iter *iter_,
static struct drgn_error *
c_format_compound_object(const struct drgn_object *obj,
struct drgn_type *underlying_type, size_t indent,
size_t multi_line_columns,
size_t one_line_columns, size_t multi_line_columns,
enum drgn_format_object_flags flags,
struct string_builder *sb)
{
@ -1128,8 +1128,10 @@ c_format_compound_object(const struct drgn_object *obj,
new->member = drgn_type_members(underlying_type);
new->end = new->member + drgn_type_num_members(underlying_type);
new->bit_offset = 0;
err = c_format_initializer(obj->prog, &iter.iter, indent, 0,
multi_line_columns, false, sb);
err = c_format_initializer(obj->prog, &iter.iter, indent,
one_line_columns, multi_line_columns,
flags & DRGN_FORMAT_OBJECT_MEMBERS_SAME_LINE,
sb);
out:
compound_initializer_stack_deinit(&iter.stack);
return err;
@ -1414,7 +1416,8 @@ c_format_array_object(const struct drgn_object *obj,
return err;
}
return c_format_initializer(obj->prog, &iter.iter, indent,
one_line_columns, multi_line_columns, true,
one_line_columns, multi_line_columns,
flags & DRGN_FORMAT_OBJECT_ELEMENTS_SAME_LINE,
sb);
}
@ -1484,6 +1487,7 @@ c_format_object_impl(const struct drgn_object *obj, size_t indent,
case DRGN_TYPE_UNION:
case DRGN_TYPE_CLASS:
return c_format_compound_object(obj, underlying_type, indent,
one_line_columns,
multi_line_columns, flags, sb);
case DRGN_TYPE_ENUM:
return c_format_enum_object(obj, underlying_type, sb);

View File

@ -976,7 +976,9 @@ static PyObject *DrgnObject_format(DrgnObject *self, PyObject *args,
X(char, DRGN_FORMAT_OBJECT_CHAR) \
X(type_name, DRGN_FORMAT_OBJECT_TYPE_NAME) \
X(member_type_names, DRGN_FORMAT_OBJECT_MEMBER_TYPE_NAMES) \
X(element_type_names, DRGN_FORMAT_OBJECT_ELEMENT_TYPE_NAMES)
X(element_type_names, DRGN_FORMAT_OBJECT_ELEMENT_TYPE_NAMES) \
X(members_same_line, DRGN_FORMAT_OBJECT_MEMBERS_SAME_LINE) \
X(elements_same_line, DRGN_FORMAT_OBJECT_ELEMENTS_SAME_LINE)
static char *keywords[] = {
#define X(name, value) #name,

View File

@ -1289,6 +1289,8 @@ class TestCPretty(ObjectTestCase):
.x = 99,
.y = -1,
}""")
self.assertEqual(obj.format_(members_same_line=True),
"(struct point){ .x = (int)99, .y = (int)-1 }")
type_ = struct_type('foo', 16, (
(point_type, 'point'),
@ -1484,6 +1486,14 @@ class TestCPretty(ObjectTestCase):
3,
4,
}""")
self.assertEqual(obj.format_(elements_same_line=False), """\
(int [5]){
0,
1,
2,
3,
4,
}""")
def test_nested_array(self):
segment = bytearray()