summaryrefslogtreecommitdiff
path: root/examples/c-to-c.py
diff options
context:
space:
mode:
authoreli.bendersky <devnull@localhost>2011-05-26 07:01:43 +0300
committereli.bendersky <devnull@localhost>2011-05-26 07:01:43 +0300
commit8348a9d2df7cd91f157578f9d503fd3c45977aa4 (patch)
tree90ada357d29aac25e85b2c6606792c1d88fd3b60 /examples/c-to-c.py
parentcad1cfde6f60288f427114be2800a5afa3ec2a62 (diff)
downloadpycparser-8348a9d2df7cd91f157578f9d503fd3c45977aa4.tar.gz
Issue 37: c-to-c: sizeof arg parenthesizing
Diffstat (limited to 'examples/c-to-c.py')
-rw-r--r--examples/c-to-c.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/examples/c-to-c.py b/examples/c-to-c.py
index ec2f04b..713cdd3 100644
--- a/examples/c-to-c.py
+++ b/examples/c-to-c.py
@@ -70,6 +70,10 @@ class CGenerator(object):
operand = self._parenthesize_unless_simple(n.expr)
if n.op == 'p++':
return '%s++' % operand
+ elif n.op == 'sizeof':
+ # Always parenthesize the argument of sizeof since it can be
+ # a name.
+ return 'sizeof(%s)' % self.visit(n.expr)
else:
return '%s%s' % (n.op, operand)
@@ -401,7 +405,13 @@ def translate_to_c(filename):
def zz_test_translate():
# internal use
src = r'''
-int main(){} '''
+int main(void)
+{
+ unsigned size;
+ size = sizeof(size);
+ return 0;
+}
+'''
parser = c_parser.CParser()
ast = parser.parse(src)
ast.show()