forked from eliben/pycparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of modifying an AST and printing out modified code
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#----------------------------------------------------------------- | ||
# pycparser: func_defs_add_param.py | ||
# | ||
# Example of rewriting AST nodes to add parameters to function | ||
# definitions. Adds an "int _hidden" to every function. | ||
# | ||
# Eli Bendersky [https://eli.thegreenplace.net/] | ||
# License: BSD | ||
#----------------------------------------------------------------- | ||
from __future__ import print_function | ||
import sys | ||
|
||
from pycparser import c_parser, c_ast, c_generator | ||
|
||
text = r""" | ||
void foo(int a, int b) { | ||
} | ||
void bar() { | ||
} | ||
""" | ||
|
||
|
||
class ParamAdder(c_ast.NodeVisitor): | ||
def visit_FuncDecl(self, node): | ||
ty = c_ast.TypeDecl(declname='_hidden', | ||
quals=[], | ||
type=c_ast.IdentifierType(['int'])) | ||
newdecl = c_ast.Decl( | ||
name='_hidden', | ||
quals=[], | ||
storage=[], | ||
funcspec=[], | ||
type=ty, | ||
init=None, | ||
bitsize=None, | ||
coord=node.coord) | ||
if node.args: | ||
node.args.params.append(newdecl) | ||
else: | ||
node.args = c_ast.ParamList(params=[newdecl]) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = c_parser.CParser() | ||
ast = parser.parse(text) | ||
print("AST before change:") | ||
ast.show(offset=2) | ||
|
||
v = ParamAdder() | ||
v.visit(ast) | ||
|
||
print("\nAST after change:") | ||
ast.show(offset=2) | ||
|
||
print("\nCode after change:") | ||
generator = c_generator.CGenerator() | ||
print(generator.visit(ast)) |