Skip to content

Commit

Permalink
Enable prog.type to work with classes
Browse files Browse the repository at this point in the history
Also added end to end CPP tests

This is a prerequisite to osandov#83

Signed-off-by: mykolal <nickolay.lysenko@gmail.com>
  • Loading branch information
prozak committed Feb 15, 2022
1 parent 7f232a4 commit 8f8fc4a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions libdrgn/build-aux/gen_c_keywords_inc_strswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"_Complex",
"char",
"const",
"class",
"double",
"enum",
"float",
Expand Down
6 changes: 5 additions & 1 deletion libdrgn/language_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ enum {
MAX_QUALIFIER_TOKEN = C_TOKEN_ATOMIC,
C_TOKEN_STRUCT,
C_TOKEN_UNION,
C_TOKEN_CLASS,
C_TOKEN_ENUM,
MAX_KEYWORD_TOKEN = C_TOKEN_ENUM,
C_TOKEN_LPAREN,
Expand Down Expand Up @@ -2107,7 +2108,8 @@ c_parse_specifier_qualifier_list(struct drgn_program *prog,
identifier_len = token.len;
} else if (token.kind == C_TOKEN_STRUCT ||
token.kind == C_TOKEN_UNION ||
token.kind == C_TOKEN_ENUM) {
token.kind == C_TOKEN_ENUM ||
token.kind == C_TOKEN_CLASS) {
if (identifier) {
return drgn_error_format(DRGN_ERROR_SYNTAX,
"cannot combine '%s' with identifier",
Expand Down Expand Up @@ -2148,6 +2150,8 @@ c_parse_specifier_qualifier_list(struct drgn_program *prog,
kind = DRGN_TYPE_UNION;
} else if (tag_token == C_TOKEN_ENUM) {
kind = DRGN_TYPE_ENUM;
} else if (tag_token == C_TOKEN_CLASS) {
kind = DRGN_TYPE_CLASS;
} else if (identifier) {
if (strstartswith(identifier, "size_t")) {
err = drgn_program_find_primitive_type(prog,
Expand Down
1 change: 1 addition & 0 deletions tests/libdrgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class C_TOKEN(enum.IntEnum):
ATOMIC = auto()
STRUCT = auto()
UNION = auto()
CLASS = auto()
ENUM = auto()
LPAREN = auto()
RPAREN = auto()
Expand Down
Binary file added tests/sample.cpp.coredump.zst
Binary file not shown.
59 changes: 59 additions & 0 deletions tests/test_e2e_cpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import os.path
import subprocess
import tempfile
import unittest

from drgn import Program, MissingDebugInfoError
from tests import TestCase


class TestE2ECPP(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
with tempfile.NamedTemporaryFile() as core_dump_file:
try:
subprocess.check_call(
[
"zstd",
"--quiet",
"--decompress",
"--stdout",
"tests/sample.cpp.coredump.zst",
],
stdout=core_dump_file,
)
except FileNotFoundError:
raise unittest.SkipTest("zstd not found")
cls.prog = Program()
cls.prog.set_core_dump(core_dump_file.name)
try:
cls.prog.load_default_debug_info()
except MissingDebugInfoError:
pass

def test_struct_e2e(self):
self.assertSequenceEqual(
"struct S {\n"
"\tint def;\n"
"\tint pub;\n"
"\tint pri;\n"
"\tint prt;\n"
"}",
self.prog.type("struct S").__str__()
)

def test_class_e2e(self):
self.assertSequenceEqual(
"class A {\n"
"\tint def;\n"
"\tint pub;\n"
"\tint pri;\n"
"\tint prt;\n"
"}",
self.prog.type("class A").__str__()
)

0 comments on commit 8f8fc4a

Please sign in to comment.