From 995da7fc3be2974a3d02a44302415884c6cd1933 Mon Sep 17 00:00:00 2001 From: Stephan Seitz <stephan.seitz@fau.de> Date: Tue, 15 Oct 2019 16:16:05 +0200 Subject: [PATCH] Add option to omit globals when printing C code --- pystencils/backends/cbackend.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py index 556065251..ed6c30075 100644 --- a/pystencils/backends/cbackend.py +++ b/pystencils/backends/cbackend.py @@ -26,7 +26,11 @@ __all__ = ['generate_c', 'CustomCodeNode', 'PrintNode', 'get_headers', 'CustomSy KERNCRAFT_NO_TERNARY_MODE = False -def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom_backend=None) -> str: +def generate_c(ast_node: Node, + signature_only: bool = False, + dialect='c', + custom_backend=None, + with_globals=True) -> str: """Prints an abstract syntax tree node as C or CUDA code. This function does not need to distinguish between C, C++ or CUDA code, it just prints 'C-like' code as encoded @@ -65,9 +69,10 @@ def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom raise ValueError("Unknown dialect: " + str(dialect)) code = printer(ast_node) if not signature_only and isinstance(ast_node, KernelFunction): - code = "\n" + code - for declaration in global_declarations: - code = printer(declaration) + "\n" + code + if with_globals and global_declarations: + code = "\n" + code + for declaration in global_declarations: + code = printer(declaration) + "\n" + code return code -- GitLab