Skip to content

Commit 076f374

Browse files
committed
Add types to several exmaples
1 parent a642470 commit 076f374

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

examples/c-to-c.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pycparser import parse_file, c_generator
1717

1818

19-
def translate_to_c(filename):
19+
def translate_to_c(filename: str) -> None:
2020
"""Simply use the c_generator module to emit a parsed AST."""
2121
ast = parse_file(filename, use_cpp=True)
2222
generator = c_generator.CGenerator()

examples/construct_ast_from_scratch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# }
2222

2323

24-
def empty_main_function_ast():
24+
def empty_main_function_ast() -> c_ast.FuncDef:
2525
constant_zero = c_ast.Constant(type="int", value="0")
2626
return_node = c_ast.Return(expr=constant_zero)
2727
compound_node = c_ast.Compound(block_items=[return_node])
@@ -46,7 +46,7 @@ def empty_main_function_ast():
4646
return main_func_node
4747

4848

49-
def generate_c_code(my_ast):
49+
def generate_c_code(my_ast: c_ast.Node) -> str:
5050
generator = c_generator.CGenerator()
5151
return generator.visit(my_ast)
5252

examples/func_calls.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818

1919
# A visitor with some state information (the funcname it's looking for)
2020
class FuncCallVisitor(c_ast.NodeVisitor):
21-
def __init__(self, funcname):
21+
def __init__(self, funcname: str) -> None:
2222
self.funcname = funcname
2323

24-
def visit_FuncCall(self, node):
25-
if node.name.name == self.funcname:
24+
def visit_FuncCall(self, node: c_ast.FuncCall) -> None:
25+
if isinstance(node.name, c_ast.ID) and node.name.name == self.funcname:
2626
print("%s called at %s" % (self.funcname, node.name.coord))
2727
# Visit args in case they contain more func calls.
28-
if node.args:
28+
if node.args is not None:
2929
self.visit(node.args)
3030

3131

32-
def show_func_calls(filename, funcname):
32+
def show_func_calls(filename: str, funcname: str) -> None:
3333
ast = parse_file(filename, use_cpp=True)
3434
v = FuncCallVisitor(funcname)
3535
v.visit(ast)

examples/func_defs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
# A simple visitor for FuncDef nodes that prints the names and
2323
# locations of function definitions.
2424
class FuncDefVisitor(c_ast.NodeVisitor):
25-
def visit_FuncDef(self, node):
25+
def visit_FuncDef(self, node: c_ast.FuncDef) -> None:
2626
print("%s at %s" % (node.decl.name, node.decl.coord))
2727

2828

29-
def show_func_defs(filename):
29+
def show_func_defs(filename: str) -> None:
3030
# Note that cpp is used. Provide a path to your own cpp or
3131
# make sure one exists in PATH.
3232
ast = parse_file(filename, use_cpp=True, cpp_args=r"-Iutils/fake_libc_include")

examples/func_defs_add_param.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
class ParamAdder(c_ast.NodeVisitor):
26-
def visit_FuncDecl(self, node):
26+
def visit_FuncDecl(self, node: c_ast.FuncDecl) -> None:
2727
ty = c_ast.TypeDecl(
2828
declname="_hidden", quals=[], align=[], type=c_ast.IdentifierType(["int"])
2929
)
@@ -38,7 +38,7 @@ def visit_FuncDecl(self, node):
3838
bitsize=None,
3939
coord=node.coord,
4040
)
41-
if node.args:
41+
if node.args is not None:
4242
node.args.params.append(newdecl)
4343
else:
4444
node.args = c_ast.ParamList(params=[newdecl])

0 commit comments

Comments
 (0)