def modifyIfStmt(self, node): statements = ASTModifier.modifyIfStmt(self, node) if len(statements) == 1: node = statements[0] condition_stmts_tuples = [] for (i, (condition, stmts)) in enumerate(node.condition_stmts_tuples): try: value = None if condition: value = evaluate_expression(condition) except ParseException: pass if value is True: # since the condition is always true it can be replaced with None, # but don't do this for if 1=1 statements since they are used as a workaround for the Kontakt 2 parser buffer overflow if not self.is1equals1(condition): condition = None if len(stmts) > 0: condition_stmts_tuples.append((condition, stmts)) break if not (value is False or len(stmts) == 0): condition_stmts_tuples.append((condition, stmts)) # if there's just an else statement left, return its statement list if len(condition_stmts_tuples) == 1 and condition_stmts_tuples[0][0] is None: return condition_stmts_tuples[0][1] elif len(condition_stmts_tuples) == 0: return [] else: node.condition_stmts_tuples = condition_stmts_tuples return [node] else: return flatten([self.modify(stmt) for stmt in statements])
def modifyIfStmt(self, node): statements = ASTModifier.modifyIfStmt(self, node) if len(statements) == 1: node = statements[0] condition_stmts_tuples = [] for (i, (condition, stmts)) in enumerate(node.condition_stmts_tuples): try: value = None if condition: value = evaluate_expression(condition) except ParseException: pass if value is True: # since the condition is always true it can be replaced with None, # but don't do this for if 1=1 statements since they are used as a workaround for the Kontakt 2 parser buffer overflow if not self.is1equals1(condition): condition = None if len(stmts) > 0: condition_stmts_tuples.append((condition, stmts)) break if not (value is False or len(stmts) == 0): condition_stmts_tuples.append((condition, stmts)) # if there's just an else statement left, return its statement list if len(condition_stmts_tuples ) == 1 and condition_stmts_tuples[0][0] is None: return condition_stmts_tuples[0][1] elif len(condition_stmts_tuples) == 0: return [] else: node.condition_stmts_tuples = condition_stmts_tuples return [node] else: return flatten([self.modify(stmt) for stmt in statements])
def modifyDeclareStmt(self, node): ''' only keep used variables ''' statements = ASTModifier.modifyDeclareStmt(self, node) if len(statements) == 1: node = statements[0] is_ui_variable = node.modifiers is not None and any([m.lower().startswith('ui_') for m in node.modifiers]) if not str(node.variable).lower() in self.used_variables and not is_ui_variable: return [] else: return [node] else: return flatten([self.modify(stmt) for stmt in statements])
def modifyWhileStmt(self, node): statements = ASTModifier.modifyWhileStmt(self, node) if len(statements) == 1: node = statements[0] try: value = evaluate_expression(node.condition) if value is False: return [] except ParseException: pass return [node] else: return flatten([self.modify(stmt) for stmt in statements])
def modifyDeclareStmt(self, node): ''' only keep used variables ''' statements = ASTModifier.modifyDeclareStmt(self, node) if len(statements) == 1: node = statements[0] is_ui_variable = node.modifiers is not None and any( [m.lower().startswith('ui_') for m in node.modifiers]) if not str(node.variable).lower( ) in self.used_variables and not is_ui_variable: return [] else: return [node] else: return flatten([self.modify(stmt) for stmt in statements])
def modifySelectStmt(self, node): statements = ASTModifier.modifySelectStmt(self, node) if len(statements) == 1: node = statements[0] try: value = evaluate_expression(node.expression) if value is None: return [node] for ((start, stop), stmts) in node.range_stmts_tuples: start = evaluate_expression(start) stop = evaluate_expression(stop) if (stop is not None and start <= value <= stop) or (start == value): return stmts except ParseException: pass return [node] else: return flatten([self.modify(stmt) for stmt in statements])
def modifyIfStmt(self, node, *args, **kwargs): # don't simplify the condition of "if 1=1" statements temp = [] for i, (condition, stmts) in enumerate(node.condition_stmts_tuples): if (isinstance(condition, BinOp) and isinstance(condition.left, Integer) and isinstance(condition.right, Integer) and i == 0 and condition.left.value == 1 and condition.right.value == 1): pass else: condition = self.modify(condition, *args, **kwargs) stmts = flatten([self.modify(s, *args, **kwargs) for s in stmts]) temp.append((condition, stmts)) if not temp: return [] else: node.condition_stmts_tuples = temp return [node]