1+ import pycparser .c_ast as c_ast
12from pycparser .c_generator import CGenerator as CGeneratorBaseBuggy
3+
24from pycparserext .ext_c_parser import FuncDeclExt , TypeDeclExt
3- import pycparser .c_ast as c_ast
45
56
67class CGeneratorBase (CGeneratorBaseBuggy ):
78 # bug fix
89 def visit_UnaryOp (self , n ):
910 operand = self ._parenthesize_unless_simple (n .expr )
10- if n .op == ' p++' :
11- return ' %s++' % operand
12- elif n .op == ' p--' :
13- return ' %s--' % operand
14- elif n .op == ' sizeof' :
11+ if n .op == " p++" :
12+ return " %s++" % operand
13+ elif n .op == " p--" :
14+ return " %s--" % operand
15+ elif n .op == " sizeof" :
1516 # Always parenthesize the argument of sizeof since it can be
1617 # a name.
17- return ' sizeof(%s)' % self .visit (n .expr )
18+ return " sizeof(%s)" % self .visit (n .expr )
1819 else :
1920 # avoid merging of "- - x" or "__real__varname"
20- return ' %s %s' % (n .op , operand )
21+ return " %s %s" % (n .op , operand )
2122
2223
2324class AsmAndAttributesMixin (object ):
@@ -48,64 +49,64 @@ def _generate_type(self, n, modifiers=None, emit_declname=True):
4849 if modifiers is None :
4950 modifiers = []
5051 typ = type (n )
51- #~ print(n, modifiers)
52+ # print(n, modifiers)
5253
5354 if typ in (c_ast .TypeDecl , TypeDeclExt ):
54- s = ''
55+ s = ""
5556 if n .quals :
56- s += ' ' .join (n .quals ) + ' '
57+ s += " " .join (n .quals ) + " "
5758 s += self .visit (n .type )
5859
59- nstr = n .declname if n .declname and emit_declname else ''
60+ nstr = n .declname if n .declname and emit_declname else ""
6061 # Resolve modifiers.
6162 # Wrap in parens to distinguish pointer to array and pointer to
6263 # function syntax.
6364 #
6465 for i , modifier in enumerate (modifiers ):
6566 if isinstance (modifier , c_ast .ArrayDecl ):
6667 if i != 0 and isinstance (modifiers [i - 1 ], c_ast .PtrDecl ):
67- nstr = '(' + nstr + ')'
68+ nstr = "(" + nstr + ")"
6869
6970 # BUG FIX: pycparser ignores quals
70- dim_quals = (' ' .join (modifier .dim_quals ) + ' '
71- if modifier .dim_quals else '' )
71+ dim_quals = (" " .join (modifier .dim_quals ) + " "
72+ if modifier .dim_quals else "" )
7273
73- nstr += '[' + dim_quals + self .visit (modifier .dim ) + ']'
74+ nstr += "[" + dim_quals + self .visit (modifier .dim ) + "]"
7475
7576 elif isinstance (modifier , c_ast .FuncDecl ):
7677 if i != 0 and isinstance (modifiers [i - 1 ], c_ast .PtrDecl ):
77- nstr = '(' + nstr + ')'
78- nstr += '(' + self .visit (modifier .args ) + ')'
78+ nstr = "(" + nstr + ")"
79+ nstr += "(" + self .visit (modifier .args ) + ")"
7980
8081 elif isinstance (modifier , FuncDeclExt ):
8182 if i != 0 and isinstance (modifiers [i - 1 ], c_ast .PtrDecl ):
82- nstr = '(' + nstr + ')'
83- nstr += '(' + self .visit (modifier .args ) + ')'
83+ nstr = "(" + nstr + ")"
84+ nstr += "(" + self .visit (modifier .args ) + ")"
8485
8586 if modifier .asm is not None :
8687 nstr += " " + self .visit (modifier .asm )
8788
8889 if modifier .attributes .exprs :
8990 nstr += (
90- ' __attribute__(('
91+ " __attribute__(("
9192 + self .visit (modifier .attributes )
92- + '))' )
93+ + "))" )
9394
9495 elif isinstance (modifier , c_ast .PtrDecl ):
9596 # BUG FIX: pycparser ignores quals
96- quals = ' ' .join (modifier .quals )
97+ quals = " " .join (modifier .quals )
9798 if quals :
98- quals = quals + ' '
99- nstr = '*' + quals + nstr
99+ quals = quals + " "
100+ nstr = "*" + quals + nstr
100101
101102 if hasattr (n , "asm" ) and n .asm :
102103 nstr += self .visit (n .asm )
103104
104105 if hasattr (n , "attributes" ) and n .attributes .exprs :
105- nstr += ' __attribute__((' + self .visit (n .attributes ) + '))'
106+ nstr += " __attribute__((" + self .visit (n .attributes ) + "))"
106107
107108 if nstr :
108- s += ' ' + nstr
109+ s += " " + nstr
109110 return s
110111
111112 elif typ == c_ast .Decl :
@@ -115,7 +116,7 @@ def _generate_type(self, n, modifiers=None, emit_declname=True):
115116 return self ._generate_type (n .type , emit_declname = emit_declname )
116117
117118 elif typ == c_ast .IdentifierType :
118- return ' ' .join (n .names ) + ' '
119+ return " " .join (n .names ) + " "
119120
120121 elif typ in (c_ast .ArrayDecl , c_ast .PtrDecl , c_ast .FuncDecl , FuncDeclExt ):
121122 return self ._generate_type (
@@ -127,7 +128,7 @@ def _generate_type(self, n, modifiers=None, emit_declname=True):
127128 def _generate_decl (self , n ):
128129 """ Generation from a Decl node.
129130 """
130- s = ''
131+ s = ""
131132
132133 def funcspec_to_str (i ):
133134 if isinstance (i , c_ast .Node ):
@@ -136,14 +137,14 @@ def funcspec_to_str(i):
136137 return i
137138
138139 if n .funcspec :
139- s = ' ' .join (funcspec_to_str (i ) for i in n .funcspec ) + ' '
140+ s = " " .join (funcspec_to_str (i ) for i in n .funcspec ) + " "
140141 if n .storage :
141- s += ' ' .join (n .storage ) + ' '
142+ s += " " .join (n .storage ) + " "
142143 s += self ._generate_type (n .type )
143144 return s
144145
145146 def visit_AttributeSpecifier (self , n ):
146- return ' __attribute__((' + self .visit (n .exprlist ) + '))'
147+ return " __attribute__((" + self .visit (n .exprlist ) + "))"
147148
148149
149150class GnuCGenerator (AsmAndAttributesMixin , CGeneratorBase ):
@@ -154,10 +155,10 @@ def visit_TypeOfExpression(self, n):
154155 return "%s(%s)" % (n .typeof_keyword , self .visit (n .expr ))
155156
156157 def visit_TypeList (self , n ):
157- return ', ' .join (self .visit (ch ) for ch in n .types )
158+ return ", " .join (self .visit (ch ) for ch in n .types )
158159
159160 def visit_RangeExpression (self , n ):
160- return ' %s ... %s' % (self .visit (n .first ), self .visit (n .last ))
161+ return " %s ... %s" % (self .visit (n .first ), self .visit (n .last ))
161162
162163
163164class GNUCGenerator (GnuCGenerator ):
@@ -169,13 +170,13 @@ def __init__(self):
169170
170171class OpenCLCGenerator (AsmAndAttributesMixin , CGeneratorBase ):
171172 def visit_FileAST (self , n ):
172- s = ''
173+ s = ""
173174 from pycparserext .ext_c_parser import PreprocessorLine
174175 for ext in n .ext :
175176 if isinstance (ext , (c_ast .FuncDef , PreprocessorLine )):
176177 s += self .visit (ext )
177178 else :
178- s += self .visit (ext ) + ' ;\n '
179+ s += self .visit (ext ) + " ;\n "
179180 return s
180181
181182 def visit_PreprocessorLine (self , n ):
0 commit comments