| 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 |
import copy |
| 36 |
import sys |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
sys.path.extend(['.', '..']) |
| 41 |
|
| 42 |
from pycparser import c_parser, c_ast |
| 43 |
|
| 44 |
|
| 45 |
def explain_c_declaration(c_decl, expand_struct=False, expand_typedef=False): |
| 46 |
""" Parses the declaration in c_decl and returns a text |
| 47 |
explanation as a string. |
| 48 |
|
| 49 |
The last external node of the string is used, to allow earlier typedefs |
| 50 |
for used types. |
| 51 |
|
| 52 |
expand_struct=True will spell out struct definitions recursively. |
| 53 |
expand_typedef=True will expand typedef'd types. |
| 54 |
""" |
| 55 |
parser = c_parser.CParser() |
| 56 |
|
| 57 |
try: |
| 58 |
node = parser.parse(c_decl, filename='<stdin>') |
| 59 |
except c_parser.ParseError: |
| 60 |
e = sys.exc_info()[1] |
| 61 |
return "Parse error:" + str(e) |
| 62 |
|
| 63 |
if (not isinstance(node, c_ast.FileAST) or |
| 64 |
not isinstance(node.ext[-1], c_ast.Decl) |
| 65 |
): |
| 66 |
return "Not a valid declaration" |
| 67 |
|
| 68 |
try: |
| 69 |
expanded = expand_struct_typedef(node.ext[-1], node, |
| 70 |
expand_struct=expand_struct, |
| 71 |
expand_typedef=expand_typedef) |
| 72 |
except Exception as e: |
| 73 |
return "Not a valid declaration: " + str(e) |
| 74 |
|
| 75 |
return _explain_decl_node(expanded) |
| 76 |
|
| 77 |
|
| 78 |
def _explain_decl_node(decl_node): |
| 79 |
""" Receives a c_ast.Decl note and returns its explanation in |
| 80 |
English. |
| 81 |
""" |
| 82 |
storage = ' '.join(decl_node.storage) + ' ' if decl_node.storage else '' |
| 83 |
|
| 84 |
return (decl_node.name + |
| 85 |
" is a " + |
| 86 |
storage + |
| 87 |
_explain_type(decl_node.type)) |
| 88 |
|
| 89 |
|
| 90 |
def _explain_type(decl): |
| 91 |
""" Recursively explains a type decl node |
| 92 |
""" |
| 93 |
typ = type(decl) |
| 94 |
|
| 95 |
if typ == c_ast.TypeDecl: |
| 96 |
quals = ' '.join(decl.quals) + ' ' if decl.quals else '' |
| 97 |
return quals + _explain_type(decl.type) |
| 98 |
elif typ == c_ast.Typename or typ == c_ast.Decl: |
| 99 |
return _explain_type(decl.type) |
| 100 |
elif typ == c_ast.IdentifierType: |
| 101 |
return ' '.join(decl.names) |
| 102 |
elif typ == c_ast.PtrDecl: |
| 103 |
quals = ' '.join(decl.quals) + ' ' if decl.quals else '' |
| 104 |
return quals + 'pointer to ' + _explain_type(decl.type) |
| 105 |
elif typ == c_ast.ArrayDecl: |
| 106 |
arr = 'array' |
| 107 |
if decl.dim: arr += '[%s]' % decl.dim.value |
| 108 |
|
| 109 |
return arr + " of " + _explain_type(decl.type) |
| 110 |
|
| 111 |
elif typ == c_ast.FuncDecl: |
| 112 |
if decl.args: |
| 113 |
params = [_explain_type(param) for param in decl.args.params] |
| 114 |
args = ', '.join(params) |
| 115 |
else: |
| 116 |
args = '' |
| 117 |
|
| 118 |
return ('function(%s) returning ' % (args) + |
| 119 |
_explain_type(decl.type)) |
| 120 |
|
| 121 |
elif typ == c_ast.Struct: |
| 122 |
decls = [_explain_decl_node(mem_decl) for mem_decl in decl.decls] |
| 123 |
members = ', '.join(decls) |
| 124 |
|
| 125 |
return ('struct%s ' % (' ' + decl.name if decl.name else '') + |
| 126 |
('containing {%s}' % members if members else '')) |
| 127 |
|
| 128 |
|
| 129 |
def expand_struct_typedef(cdecl, file_ast, |
| 130 |
expand_struct=False, |
| 131 |
expand_typedef=False): |
| 132 |
"""Expand struct & typedef and return a new expanded node.""" |
| 133 |
decl_copy = copy.deepcopy(cdecl) |
| 134 |
_expand_in_place(decl_copy, file_ast, expand_struct, expand_typedef) |
| 135 |
return decl_copy |
| 136 |
|
| 137 |
|
| 138 |
def _expand_in_place(decl, file_ast, expand_struct=False, expand_typedef=False): |
| 139 |
"""Recursively expand struct & typedef in place, throw RuntimeError if |
| 140 |
undeclared struct or typedef are used |
| 141 |
""" |
| 142 |
typ = type(decl) |
| 143 |
|
| 144 |
if typ in (c_ast.Decl, c_ast.TypeDecl, c_ast.PtrDecl, c_ast.ArrayDecl): |
| 145 |
decl.type = _expand_in_place(decl.type, file_ast, expand_struct, |
| 146 |
expand_typedef) |
| 147 |
|
| 148 |
elif typ == c_ast.Struct: |
| 149 |
if not decl.decls: |
| 150 |
struct = _find_struct(decl.name, file_ast) |
| 151 |
if not struct: |
| 152 |
raise RuntimeError('using undeclared struct %s' % decl.name) |
| 153 |
decl.decls = struct.decls |
| 154 |
|
| 155 |
for i, mem_decl in enumerate(decl.decls): |
| 156 |
decl.decls[i] = _expand_in_place(mem_decl, file_ast, expand_struct, |
| 157 |
expand_typedef) |
| 158 |
if not expand_struct: |
| 159 |
decl.decls = [] |
| 160 |
|
| 161 |
elif (typ == c_ast.IdentifierType and |
| 162 |
decl.names[0] not in ('int', 'char')): |
| 163 |
typedef = _find_typedef(decl.names[0], file_ast) |
| 164 |
if not typedef: |
| 165 |
raise RuntimeError('using undeclared type %s' % decl.names[0]) |
| 166 |
|
| 167 |
if expand_typedef: |
| 168 |
return typedef.type |
| 169 |
|
| 170 |
return decl |
| 171 |
|
| 172 |
|
| 173 |
def _find_struct(name, file_ast): |
| 174 |
"""Receives a struct name and return declared struct object in file_ast |
| 175 |
""" |
| 176 |
for node in file_ast.ext: |
| 177 |
if (type(node) == c_ast.Decl and |
| 178 |
type(node.type) == c_ast.Struct and |
| 179 |
node.type.name == name): |
| 180 |
return node.type |
| 181 |
|
| 182 |
|
| 183 |
def _find_typedef(name, file_ast): |
| 184 |
"""Receives a type name and return typedef object in file_ast |
| 185 |
""" |
| 186 |
for node in file_ast.ext: |
| 187 |
if type(node) == c_ast.Typedef and node.name == name: |
| 188 |
return node |
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__": |
| 192 |
if len(sys.argv) > 1: |
| 193 |
c_decl = sys.argv[1] |
| 194 |
else: |
| 195 |
c_decl = "char *(*(**foo[][8])())[];" |
| 196 |
|
| 197 |
print("Explaining the declaration: " + c_decl + "\n") |
| 198 |
print(explain_c_declaration(c_decl) + "\n") |