From 79f2df20613395472cad4b4cd1b2d53b6d3a8aae Mon Sep 17 00:00:00 2001 From: KoenVda88 <66839640+KoenVda88@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:35:18 +0200 Subject: [PATCH] Update load.py Fix for casts like "int test[ (uint8_t)1 + 2]" --- load.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/load.py b/load.py index 96963b1..ab61f9c 100644 --- a/load.py +++ b/load.py @@ -218,7 +218,25 @@ def set_SourceContent(self, source_content): def visit_Decl(self, n, *args, **kwargs): result = super().visit_Decl(n, *args, **kwargs) - if isinstance(n.type, pycparser.c_ast.FuncDecl): + if isinstance(n.type, pycparser.c_ast.ArrayDecl): + array_size = result[result.find("[")+1:result.find("]")] + + # cffi doesn't work with array sizes that are not a number. like "int test[ 1 + 2]" + # So eval the number + if( not array_size.isnumeric() ): + + # remove possible casts like "int test[ (uint8_t)1 + 2]" + no_cast_array_size = re.sub("\\(\\s*[u]int[0-9]*_t\\s*\\)", " ", array_size) + try: + number = round(eval(no_cast_array_size)) + + # remove the size + result = result.replace(array_size, str(number)) + except: + print("can't parse: " + result) + pass + + elif isinstance(n.type, pycparser.c_ast.FuncDecl): # Is a function declaration if n.name in self.functions: # Is already in functions