Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
fixup! annotate_ifdef_directives: remove some cases of double negation
Fix our logic for removing double negation, thanks to tests added
later in the branch.
Loading branch information
Showing
1 changed file
with
31 additions
and
5 deletions .
+31
−5
scripts/maint/annotate_ifdef_directives
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
@@ -55,16 +55,42 @@ def commented_line(fmt, argument, maxwidth=LINE_WIDTH):
return result
def negate (expr ):
"""Return a negated version of expr; try to avoid double-negation."""
"""Return a negated version of expr; try to avoid double-negation.
We usually wrap expressions in parentheses and add a "!".
>>> negate("A && B")
'!(A && B)'
But if we recognize the expression as negated, we can restore it.
>>> negate(negate("A && B"))
'A && B'
The same applies for defined(FOO).
>>> negate("defined(FOO)")
'!defined(FOO)'
>>> negate(negate("defined(FOO)"))
'defined(FOO)'
Internal parentheses don't confuse us:
>>> negate("!(FOO) && !(BAR)")
'!(!(FOO) && !(BAR))'
"""
expr = expr .strip ()
# See whether we match !(...), with no intervening close-parens.
m = re .match (r'^!\s*\(([^\)*] )\)$' , expr )
m = re .match (r'^!\s*\(([^\)]* )\)$' , expr )
if m :
return m .group (1 )
# See whether we match !defined(...), with no intervening close-parens.
m = re .match (r'^!\s*(defined\([^\)]*\))$' , expr )
# See whether we match !?defined(...), with no intervening close-parens.
m = re .match (r'^(!?)\s*(defined\([^\)]*\))$' , expr )
if m :
return m .group (1 )
if m .group (1 ) == "!" :
prefix = ""
else :
prefix = "!"
return prefix + m .group (2 )
return "!(%s)" % expr
Toggle all file notes
Toggle all file annotations