AdaCore / AdaCore/libadalang

helper function to list properties of a node

Open
#317 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Ada
Stars
171
Forks
48
PR merge metrics
No merged PRs in 30d

Description

When developing with libadalang/python, I have found the following function very useful. I just pass it a node, and it dumps all useful properties on the console. I wonder whether the libadalang developers already have something similar or better that could be made more official... A small part of the complexity here is to work around a few libadalang crashes when calling some of the functions (although I have noticed less and less of these in the last few months :-)

def dump(v):
    """
    Debug only
    """
    import inspect

    methods = set()
    staticmethods = set()
    fields = {}

    for name in dir(v):
        if name in ('p_int_type', 'p_bool_type', 'dump', 'dump_str',
                    'p_universal_int_type', 'p_universal_real_type',
                    'find', 'findall', 'finditer', 'is_a',
                    'p_complete', 'p_gnat_xref', 'p_as_symbol_array',
                    'p_referenced_decl_internal',
                    ):
            continue

        if name.startswith('_'):
            continue

        try:
            value = getattr(v, name)
        except:
            value = '??? Error when computing'

        if inspect.ismethod(value):
            methods.add(name)
        elif inspect.isfunction(value):
            staticmethods.add(name)
        else:
            fields[name] = value

    print("=== Dump: %s" % v)
    print("  inheritance tree: %s" %
            map(lambda n: n.__name__, inspect.getmro(type(v))))
    if methods:
        print("  methods:          %s" % sorted(methods))
    if staticmethods:
        print("  staticmethods:    %s" % sorted(staticmethods))

    for name in sorted(fields):
        value = fields[name]
        if name in ('p_referenced_decl', 'p_referenced_id',
                    'p_is_dot_call', 'p_is_static_expr', 'p_name_is',
                    ):
            try:
                name = name + '()'
                value = value()
            except:
                value = '??? Error when calling'

        try:
            value = str(value)
        except:
            value = "??? Exception when converting to string"

        print('  %-18s: %s' % (name, value))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied dump(v) helper and the libadalang/python node API, then check how the listed attributes behave when inspected. Done means an official equivalent is defined for dumping useful node properties and the known crash cases are handled or documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.