summaryrefslogtreecommitdiff
path: root/python/c++filt.py
blob: 6ec81946373549dbdc88af82521eb76d6720e55c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import argparse
import sys
import pychrysalide
from pychrysalide.analysis.types import TemplateType
from pychrysalide.mangling import ItaniumDemangler



def display_template_params(root):
    """Look for template parameters in a type and its namespace."""

    if root:

        got = display_template_params(root.namespace)

        if isinstance(root, TemplateType):

            for p in root.params:
                print(' -', p, '(', type(p), ')')

            got = True

    else:

        got = False

    return got


if __name__ == '__main__':
    """Entry point."""

    parser = argparse.ArgumentParser(description='c++filt - Demangle C++ Itanium symbols.', add_help=False)

    parser.add_argument('-h', '--help', action='store_true', help='Display the command line options.')

    parser.add_argument('symbol', type=str, help='Symbol to be demangled')

    args = parser.parse_args()

    if args.help:
        parser.print_help()
        sys.exit(1)

    demangler = ItaniumDemangler()

    demangled = demangler.decode_routine(args.symbol)

    if demangled is None:
        print('Error')

    else:

        print()

        print(demangled)
        print()

        print('Namespace')
        print('---------')

        print(' -', demangled.namespace, '(', type(demangled.namespace), ')')
        print()

        print('Template parameters')
        print('-------------------')

        got = display_template_params(demangled.namespace)

        if demangled.typed_name:
            got |= display_template_params(demangled.typed_name)

        if not(got):
            print(' - (none)')

        print()

        print('Name')
        print('----')

        if demangled.typed_name:
            print(' -', demangled.typed_name, '(', type(demangled.typed_name), ')')

        else:
            print(' -', demangled.name, '(', type(demangled.name), ')')

        print()

        print('Return')
        print('------')

        print(' -', demangled.ret, '(', type(demangled.ret), ')')
        print()

        print('Arguments')
        print('---------')

        args = demangled.args

        if len(args) == 0:
            print(' - (none)')

        else:

            count = 0

            for a in args:
                print(' - [%d] -' % count, a.type, '(', type(a.type), ')')
                count += 1

        print()