From b7e6e6f7b125bc48ce910690ad721994ebdf177d Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Sat, 7 May 2016 03:18:50 +0200 Subject: [PATCH] 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. --- lslopt/lslfoldconst.py | 16 ++++++++-------- lslopt/lsloptimizer.py | 5 +++-- lslopt/lsloutput.py | 13 ++++++++----- main.py | 7 ++++--- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lslopt/lslfoldconst.py b/lslopt/lslfoldconst.py index 834ce1f..f1239af 100644 --- a/lslopt/lslfoldconst.py +++ b/lslopt/lslfoldconst.py @@ -945,14 +945,14 @@ class foldconst(object): else: value = fn(*tuple(arg['value'] for arg in child)) if not self.foldtabs: - if not self.nofoldtabs: - generatesTabs = ( - isinstance(value, unicode) and '\t' in value - or type(value) == list and any(isinstance(x, unicode) and '\t' in x for x in value) - ) - if generatesTabs: - 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']) - return + generatesTabs = ( + isinstance(value, unicode) and '\t' in value + or type(value) == list and any(isinstance(x, unicode) and '\t' in x for x in value) + ) + 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 by disabling the 'warntabs' option)." % node['name']) + return parent[index] = {'nt':'CONST', 't':node['t'], 'value':value} except lslfuncs.ELSLCantCompute: # Don't transform the tree if function is not computable diff --git a/lslopt/lsloptimizer.py b/lslopt/lsloptimizer.py index d47efe8..c90d7c4 100644 --- a/lslopt/lsloptimizer.py +++ b/lslopt/lsloptimizer.py @@ -57,7 +57,8 @@ class optimizer(foldconst, renamer, deadcode): ret['X'] = value['X'] 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 predefined functions for folding constants. """ @@ -68,7 +69,7 @@ class optimizer(foldconst, renamer, deadcode): self.addstrings = 'addstrings' in options self.foldtabs = 'foldtabs' in options - self.nofoldtabs = 'nofoldtabs' in options + self.warntabs = 'warntabs' in options self.shrinknames = 'shrinknames' in options diff --git a/lslopt/lsloutput.py b/lslopt/lsloutput.py index 93ad34d..800df86 100644 --- a/lslopt/lsloutput.py +++ b/lslopt/lsloutput.py @@ -53,9 +53,11 @@ class outscript(object): else: pfx = '((key)' sfx = ')' - if '\t' in value: - warning('A string contains a tab. Tabs are expanded to four' - ' spaces by the viewer when copy-pasting the code.') + if '\t' in value and self.warntabs: + warning("A string contains a tab. Tabs are expanded to four" + " spaces by the viewer when copy-pasting the code" + " (disable this warning by disabling the 'warntabs'" + " option).") return pfx + '"' + value.encode('utf8').replace('\\','\\\\') \ .replace('"','\\"').replace('\n','\\n') + '"' + sfx if tvalue == int: @@ -428,14 +430,15 @@ class outscript(object): 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 self.tree, self.symtab = treesymtab - # Optimize signs + # Grab options self.optsigns = 'optsigns' in options self.optfloats = 'optfloats' in options self.foldconst = 'constfold' in options + self.warntabs = 'warntabs' in options ret = '' self.indent = ' ' diff --git a/main.py b/main.py index 3d2f7a9..85124ae 100755 --- a/main.py +++ b/main.py @@ -281,8 +281,9 @@ Optimizer options (+ means active by default, - means inactive by default): expansion of functions that produce strings with tabs. The resulting source isn't guaranteed to be copy-paste-able to the viewer. - nofoldtabs - Suppress warning when a function can't be optimized - because it generates a string or list with a tab. + warntabs + Suppress warning when a function can't be optimized + 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 were comments. Not useful unless the script is itself the output of a preprocessor like GNU cpp, which inserts @@ -303,7 +304,7 @@ def main(): # Default options options = set(('extendedglobalexpr','extendedtypecast','extendedassignment', - 'allowkeyconcat','allowmultistrings','skippreproc','optimize', + 'allowkeyconcat','allowmultistrings','skippreproc','warntabs','optimize', 'optsigns','optfloats','constfold','dcr','errmissingdefault', ))