2 # Copyright (c) 2017-2019, The Tor Project, Inc.
3 # See LICENSE for licensing information
6 This script iterates over a list of C files. For each file, it looks at the
7 #if/#else C macros, and annotates them with comments explaining what they
10 For example, it replaces this kind of input...
13 ... #ifdef HAVE_OCELOT
15 ... #if MIMSY == BOROGROVE
29 With this kind of output:
30 >>> EXPECTED_OUTPUT = '''
31 ... #ifdef HAVE_OCELOT
33 ... #if MIMSY == BOROGROVE
38 ... #else /* !(MIMSY == BOROGROVE) */
43 ... #endif /* MIMSY == BOROGROVE */
44 ... #endif /* defined(HAVE_OCELOT) */
49 >>> if sys.version_info.major < 3: from cStringIO import StringIO
50 >>> if sys.version_info.major >= 3: from io import StringIO
52 >>> OUTPUT = StringIO()
53 >>> translate(StringIO(INPUT), OUTPUT)
54 >>> assert OUTPUT.getvalue() == EXPECTED_OUTPUT
56 Note that only #else and #endif lines are annotated. Existing comments
57 on those lines are removed.
60 # Future imports for Python 2.7, mandatory in 3.0
61 from __future__
import division
62 from __future__
import print_function
63 from __future__
import unicode_literals
67 # Any block with fewer than this many lines does not need annotations.
68 LINE_OBVIOUSNESS_LIMIT
= 4
70 # Maximum line width. This includes a terminating newline character.
72 # (This is the maximum before encoding, so that if the the operating system
73 # uses multiple characters to encode newline, that's still okay.)
76 class Problem(Exception):
79 def close_parens_needed(expr
):
80 """Return the number of left-parentheses needed to make 'expr'
83 >>> close_parens_needed("1+2")
85 >>> close_parens_needed("(1 + 2)")
87 >>> close_parens_needed("(1 + 2")
89 >>> close_parens_needed("(1 + (2 *")
91 >>> close_parens_needed("(1 + (2 * 3) + (4")
94 return expr
.count("(") - expr
.count(")")
96 def truncate_expression(expr
, new_width
):
97 """Given a parenthesized C expression in 'expr', try to return a new
98 expression that is similar to 'expr', but no more than 'new_width'
101 Try to return an expression with balanced parentheses.
103 >>> truncate_expression("1+2+3", 8)
105 >>> truncate_expression("1+2+3+4+5", 8)
107 >>> truncate_expression("(1+2+3+4)", 8)
109 >>> truncate_expression("(1+(2+3+4))", 8)
111 >>> truncate_expression("(((((((((", 8)
114 if len(expr
) <= new_width
:
115 # The expression is already short enough.
120 # Start this at the minimum that we might truncate.
121 n_to_remove
= len(expr
) + len(ellipsis
) - new_width
123 # Try removing characters, one by one, until we get something where
124 # re-balancing the parentheses still fits within the limit.
125 while n_to_remove
< len(expr
):
126 truncated
= expr
[:-n_to_remove
] + ellipsis
127 truncated
+= ")" * close_parens_needed(truncated
)
128 if len(truncated
) <= new_width
:
134 def commented_line(fmt
, argument
, maxwidth
=LINE_WIDTH
):
135 # (This is a raw docstring so that our doctests can use \.)
137 Return fmt%argument, for use as a commented line. If the line would
138 be longer than maxwidth, truncate argument but try to keep its
139 parentheses balanced.
141 Requires that fmt%"..." will fit into maxwidth characters.
143 Requires that fmt ends with a newline.
145 >>> commented_line("/* %s */\n", "hello world", 32)
146 '/* hello world */\n'
147 >>> commented_line("/* %s */\n", "hello world", 15)
149 >>> commented_line("#endif /* %s */\n", "((1+2) && defined(FOO))", 32)
150 '#endif /* ((1+2) && defi...) */\n'
153 The default line limit is 80 characters including the newline:
155 >>> long_argument = "long " * 100
156 >>> long_line = commented_line("#endif /* %s */\n", long_argument)
161 '#endif /* long long long long long long '
163 'long long long long long long lon... */\n'
165 If a line works out to being 80 characters naturally, it isn't truncated,
166 and no ellipsis is added.
168 >>> medium_argument = "a"*66
169 >>> medium_line = commented_line("#endif /* %s */\n", medium_argument)
172 >>> "..." in medium_line
175 '#endif /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
177 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n'
181 assert fmt
.endswith("\n")
182 result
= fmt
% argument
183 if len(result
) <= maxwidth
:
186 # How long can we let the argument be? Try filling in the
187 # format with an empty argument to find out.
188 max_arg_width
= maxwidth
- len(fmt
% "")
189 result
= fmt
% truncate_expression(argument
, max_arg_width
)
190 assert len(result
) <= maxwidth
194 """Return a negated version of expr; try to avoid double-negation.
196 We usually wrap expressions in parentheses and add a "!".
200 But if we recognize the expression as negated, we can restore it.
201 >>> negate(negate("A && B"))
204 The same applies for defined(FOO).
205 >>> negate("defined(FOO)")
207 >>> negate(negate("defined(FOO)"))
210 Internal parentheses don't confuse us:
211 >>> negate("!(FOO) && !(BAR)")
212 '!(!(FOO) && !(BAR))'
216 # See whether we match !(...), with no intervening close-parens.
217 m
= re
.match(r
'^!\s*\(([^\)]*)\)$', expr
)
222 # See whether we match !?defined(...), with no intervening close-parens.
223 m
= re
.match(r
'^(!?)\s*(defined\([^\)]*\))$', expr
)
225 if m
.group(1) == "!":
229 return prefix
+ m
.group(2)
231 return "!(%s)" % expr
235 Remove existing trailing comments from an #else or #endif line.
237 s
= re
.sub(r
'//.*','',s
)
238 s
= re
.sub(r
'/\*.*','',s
)
241 def translate(f_in
, f_out
):
243 Read a file from f_in, and write its annotated version to f_out.
245 # A stack listing our current if/else state. Each member of the stack
246 # is a list of directives. Each directive is a 3-tuple of
247 # (command, rest, lineno)
248 # where "command" is one of if/ifdef/ifndef/else/elif, and where
249 # "rest" is an expression in a format suitable for use with #if, and where
250 # lineno is the line number where the directive occurred.
252 # the stack element corresponding to the top level of the file.
254 cur_level
= whole_file
258 m
= re
.match(r
'\s*#\s*(if|ifdef|ifndef|else|endif|elif)\b\s*(.*)',
261 # no directive, so we can just write it out.
264 command
,rest
= m
.groups()
265 if command
in ("if", "ifdef", "ifndef"):
266 # The #if directive pushes us one level lower on the stack.
267 if command
== 'ifdef':
268 rest
= "defined(%s)"%uncomment
(rest
)
269 elif command
== 'ifndef':
270 rest
= "!defined(%s)"%uncomment
(rest
)
271 elif rest
.endswith("\\"):
272 rest
= rest
[:-1]+"..."
274 rest
= uncomment(rest
)
276 new_level
= [ (command
, rest
, lineno
) ]
277 stack
.append(cur_level
)
278 cur_level
= new_level
280 elif command
in ("else", "elif"):
281 # We stay at the same level on the stack. If we have an #else,
283 if len(cur_level
) == 0 or cur_level
[-1][0] == 'else':
284 raise Problem("Unexpected #%s on %d"% (command
,lineno
))
285 if (len(cur_level
) == 1 and command
== 'else' and
286 lineno
> cur_level
[0][2] + LINE_OBVIOUSNESS_LIMIT
):
287 f_out
.write(commented_line("#else /* %s */\n",
288 negate(cur_level
[0][1])))
291 cur_level
.append((command
, rest
, lineno
))
293 # We pop one element on the stack, and comment an endif.
294 assert command
== 'endif'
296 raise Problem("Unmatched #%s on %s"% (command
,lineno
))
297 if lineno
<= cur_level
[0][2] + LINE_OBVIOUSNESS_LIMIT
:
299 elif len(cur_level
) == 1 or (
300 len(cur_level
) == 2 and cur_level
[1][0] == 'else'):
301 f_out
.write(commented_line("#endif /* %s */\n",
304 f_out
.write(commented_line("#endif /* %s || ... */\n",
306 cur_level
= stack
.pop()
307 if len(stack
) or cur_level
!= whole_file
:
308 raise Problem("Missing #endif")
310 if __name__
== '__main__':
314 if sys
.argv
[1] == "--self-test":
319 for fn
in sys
.argv
[1:]:
320 with
open(fn
+"_OUT", 'w') as output_file
:
321 translate(open(fn
, 'r'), output_file
)
322 os
.rename(fn
+"_OUT", fn
)