libdrgn: Add cpp language and tests

This commit is contained in:
Jay Kamat 2020-03-02 16:01:38 -08:00 committed by Omar Sandoval
parent fa61977f60
commit d8fadf10ee
5 changed files with 55 additions and 0 deletions

View File

@ -39,6 +39,40 @@ const struct drgn_language drgn_languages[] = {
.op_neg = c_op_neg,
.op_not = c_op_not,
},
[DRGN_LANGUAGE_CPP] = {
.name = "C++",
.void_type = {
{
.kind = DRGN_TYPE_VOID,
.primitive = DRGN_C_TYPE_VOID,
.language = &drgn_language_cpp,
},
},
.format_type_name = c_format_type_name,
.format_type = c_format_type,
.format_object = c_format_object,
.find_type = c_find_type,
.bit_offset = c_bit_offset,
.integer_literal = c_integer_literal,
.bool_literal = c_bool_literal,
.float_literal = c_float_literal,
.op_cast = c_op_cast,
.op_bool = c_op_bool,
.op_cmp = c_op_cmp,
.op_add = c_op_add,
.op_sub = c_op_sub,
.op_mul = c_op_mul,
.op_div = c_op_div,
.op_mod = c_op_mod,
.op_lshift = c_op_lshift,
.op_rshift = c_op_rshift,
.op_and = c_op_and,
.op_or = c_op_or,
.op_xor = c_op_xor,
.op_pos = c_op_pos,
.op_neg = c_op_neg,
.op_not = c_op_not,
},
};
struct drgn_error *drgn_language_from_die(Dwarf_Die *die,
@ -55,6 +89,12 @@ struct drgn_error *drgn_language_from_die(Dwarf_Die *die,
case DW_LANG_C11:
*ret = &drgn_language_c;
break;
case DW_LANG_C_plus_plus:
case DW_LANG_C_plus_plus_03:
case DW_LANG_C_plus_plus_11:
case DW_LANG_C_plus_plus_14:
*ret = &drgn_language_cpp;
break;
default:
*ret = NULL;
break;

View File

@ -158,11 +158,13 @@ drgn_unary_op c_op_not;
enum {
DRGN_LANGUAGE_C,
DRGN_LANGUAGE_CPP,
DRGN_NUM_LANGUAGES,
};
extern const struct drgn_language drgn_languages[DRGN_NUM_LANGUAGES];
#define drgn_language_cpp drgn_languages[DRGN_LANGUAGE_CPP]
#define drgn_language_c drgn_languages[DRGN_LANGUAGE_C]
/**

View File

@ -60,6 +60,7 @@ int add_languages(void)
{
static const char *attr_names[] = {
[DRGN_LANGUAGE_C] = "C",
[DRGN_LANGUAGE_CPP] = "CPP",
};
size_t i;

View File

@ -2060,6 +2060,14 @@ class TestProgram(unittest.TestCase):
self.assertEqual(
dwarf_program(dies, lang=DW_LANG.BLISS).language, DEFAULT_LANGUAGE
)
self.assertEqual(
dwarf_program(dies, lang=DW_LANG.C_plus_plus_14).language, Language.CPP
)
self.assertEqual(
dwarf_program(dies, lang=DW_LANG.C_plus_plus).object("main").type_.language,
Language.CPP,
)
def test_reference_counting(self):
# Test that we keep the appropriate objects alive even if we don't have

View File

@ -1165,6 +1165,10 @@ class TestType(unittest.TestCase):
self.assertEqual(void_type(language=None).language, DEFAULT_LANGUAGE)
self.assertEqual(void_type(language=Language.C).language, Language.C)
self.assertEqual(
int_type("int", 4, True, language=Language.CPP).language, Language.CPP
)
def test_cmp(self):
self.assertEqual(void_type(), void_type())
self.assertEqual(void_type(Qualifiers.CONST), void_type(Qualifiers.CONST))