Fix bug with tab handling.

Commit 5804a9a introduced a bug where having the foldtabs option disabled (normal) prevented optimizations of functions. Fix it for good (hopefully). While on it, rename the nofoldtabs option to warntabs, making it default, and use it to disable a warning when there are tabs in a string.
This commit is contained in:
Sei Lisa 2016-05-07 03:18:50 +02:00
parent 95bd3209be
commit b7e6e6f7b1
4 changed files with 23 additions and 18 deletions

View file

@ -945,14 +945,14 @@ class foldconst(object):
else: else:
value = fn(*tuple(arg['value'] for arg in child)) value = fn(*tuple(arg['value'] for arg in child))
if not self.foldtabs: if not self.foldtabs:
if not self.nofoldtabs: generatesTabs = (
generatesTabs = ( isinstance(value, unicode) and '\t' in value
isinstance(value, unicode) and '\t' in value or type(value) == list and any(isinstance(x, unicode) and '\t' in x for x in value)
or type(value) == list and any(isinstance(x, unicode) and '\t' in x for x in value) )
) if generatesTabs:
if generatesTabs: if self.warntabs:
warning("Can't optimize call to %s because it would generate a tab character (you can force the optimization with the foldtabs option, or disable this warning with the nofoldtabs option)." % node['name']) warning("Can't optimize call to %s because it would generate a tab character (you can force the optimization with the 'foldtabs' option, or disable this warning by disabling the 'warntabs' option)." % node['name'])
return return
parent[index] = {'nt':'CONST', 't':node['t'], 'value':value} parent[index] = {'nt':'CONST', 't':node['t'], 'value':value}
except lslfuncs.ELSLCantCompute: except lslfuncs.ELSLCantCompute:
# Don't transform the tree if function is not computable # Don't transform the tree if function is not computable

View file

@ -57,7 +57,8 @@ class optimizer(foldconst, renamer, deadcode):
ret['X'] = value['X'] ret['X'] = value['X']
return ret return ret
def optimize(self, treesymtab, options = ('optimize','constfold','dcr')): def optimize(self, treesymtab, options = ('optimize','constfold','dcr',
'warntabs')):
"""Optimize the symbolic table symtab in place. Requires a table of """Optimize the symbolic table symtab in place. Requires a table of
predefined functions for folding constants. predefined functions for folding constants.
""" """
@ -68,7 +69,7 @@ class optimizer(foldconst, renamer, deadcode):
self.addstrings = 'addstrings' in options self.addstrings = 'addstrings' in options
self.foldtabs = 'foldtabs' in options self.foldtabs = 'foldtabs' in options
self.nofoldtabs = 'nofoldtabs' in options self.warntabs = 'warntabs' in options
self.shrinknames = 'shrinknames' in options self.shrinknames = 'shrinknames' in options

View file

@ -53,9 +53,11 @@ class outscript(object):
else: else:
pfx = '((key)' pfx = '((key)'
sfx = ')' sfx = ')'
if '\t' in value: if '\t' in value and self.warntabs:
warning('A string contains a tab. Tabs are expanded to four' warning("A string contains a tab. Tabs are expanded to four"
' spaces by the viewer when copy-pasting the code.') " spaces by the viewer when copy-pasting the code"
" (disable this warning by disabling the 'warntabs'"
" option).")
return pfx + '"' + value.encode('utf8').replace('\\','\\\\') \ return pfx + '"' + value.encode('utf8').replace('\\','\\\\') \
.replace('"','\\"').replace('\n','\\n') + '"' + sfx .replace('"','\\"').replace('\n','\\n') + '"' + sfx
if tvalue == int: if tvalue == int:
@ -428,14 +430,15 @@ class outscript(object):
assert False, "Internal error: node type not handled: " + nt # pragma: no cover assert False, "Internal error: node type not handled: " + nt # pragma: no cover
def output(self, treesymtab, options = ('optsigns','optfloats')): def output(self, treesymtab, options = ('optsigns','optfloats','warntabs')):
# Build a sorted list of dict entries # Build a sorted list of dict entries
self.tree, self.symtab = treesymtab self.tree, self.symtab = treesymtab
# Optimize signs # Grab options
self.optsigns = 'optsigns' in options self.optsigns = 'optsigns' in options
self.optfloats = 'optfloats' in options self.optfloats = 'optfloats' in options
self.foldconst = 'constfold' in options self.foldconst = 'constfold' in options
self.warntabs = 'warntabs' in options
ret = '' ret = ''
self.indent = ' ' self.indent = ' '

View file

@ -281,8 +281,9 @@ Optimizer options (+ means active by default, - means inactive by default):
expansion of functions that produce strings with tabs. expansion of functions that produce strings with tabs.
The resulting source isn't guaranteed to be The resulting source isn't guaranteed to be
copy-paste-able to the viewer. copy-paste-able to the viewer.
nofoldtabs - Suppress warning when a function can't be optimized warntabs + Suppress warning when a function can't be optimized
because it generates a string or list with a tab. because it generates a string or list with a tab, or
when a string contains a tab.
skippreproc + Skip preprocessor directives in the source as if they skippreproc + Skip preprocessor directives in the source as if they
were comments. Not useful unless the script is itself were comments. Not useful unless the script is itself
the output of a preprocessor like GNU cpp, which inserts the output of a preprocessor like GNU cpp, which inserts
@ -303,7 +304,7 @@ def main():
# Default options # Default options
options = set(('extendedglobalexpr','extendedtypecast','extendedassignment', options = set(('extendedglobalexpr','extendedtypecast','extendedassignment',
'allowkeyconcat','allowmultistrings','skippreproc','optimize', 'allowkeyconcat','allowmultistrings','skippreproc','warntabs','optimize',
'optsigns','optfloats','constfold','dcr','errmissingdefault', 'optsigns','optfloats','constfold','dcr','errmissingdefault',
)) ))