From b5e5d82bb209d39f014a1f3150f7e0f253564d31 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 28 Jun 2015 12:42:21 +0200 Subject: cleanup: detect bracket on new line, try else handling Note: else heuristics is weak... g_hash_table_for_each has an unchecked parameter which needs manual handling. --- one-off/cleanup-rewrite.py | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'one-off') diff --git a/one-off/cleanup-rewrite.py b/one-off/cleanup-rewrite.py index f87cfb4..364bd47 100755 --- a/one-off/cleanup-rewrite.py +++ b/one-off/cleanup-rewrite.py @@ -82,8 +82,12 @@ RE_IF = re.compile( RE_REASS = re.compile('reassembly_table_init\s*\(\s*(?P[^\s,]+)') # Matches "g_hash_table_destroy(HT_NAME)" RE_HT_DESTROY = re.compile(r''' - g_hash_table_destroy\s*\(\s* # "g_hash_table_destroy(" + (?: + g_hash_table_foreach_remove| + g_hash_table_destroy + )\s*\(\s* # "g_hash_table_destroy(" (?P[.\w]+)\s* # "struct.ht_name" + [^)]* # params for g_hash_table_foreach_remove \) # ")" ''', re.X) RE_ASSIGNMENT = re.compile(r'(?P[.\w]+)\s*=\s*(?P[^;]*)') @@ -209,11 +213,11 @@ class Function(object): else: # Look for if (...) ...; line, text = self._read_stmt(line, ';') + # Handle newline between "if (...)" and "{" + if '{' in text: + line, text = self._read_stmt(line, '}') - # Check for else that is not understood. - if re.search('\}\s*else\b', text): - self.unknown_lines += line - return True # Cannot handle else yet! True to avoid double append + has_else = re.search(r'\}\s*else\b', text) # Get rid of if condition and brackets if '{' in text: @@ -250,6 +254,24 @@ class Function(object): self.unknown_lines += line return True + # If an else is present, continue searching for more + if has_else: + line, text = self._read_stmt('', '}') + if 'g_hash_table_new' in text: + line = line.split('}', 1)[0] + indent = self.get_indent() + line = ''.join([l.replace(indent, '', 1) + for l in line.splitlines(True)]) + text = line + _logger.debug('Found hash table in else: %s', text) + if varName not in self.ht_names: + _logger.warn('HT %s was not destructed', varName) + #self.ht_names.append(varName) + self.lines_keep += line + else: + # Hmm... no idea what this is. + self.unknown_lines += line + return True def _is_ht_name(self, varName): @@ -400,8 +422,10 @@ class Source(object): # Matches " register_init_routine (&foo_init);" caller_match = re.search( r''' - ^([ \t]*)register_init_routine\s* - \(\s* &? \s*(?P\w+)\s* \);\n + ^(?P + (?:[ \t]*)register_init_routine\s* + \(\s* &? \s*(?P\w+)\s* \); + )[^\n]*\n ''', block, re.M | re.X) if not caller_match: # Sanity check @@ -416,9 +440,10 @@ class Source(object): return # Yields " register_cleanup_routine (&foo_cleanup);" - extra_line = caller_match.group() \ + extra_line = caller_match.group('line') \ .replace('register_init_routine', 'register_cleanup_routine') \ .replace(funcName, cleanupFuncName) + extra_line += '\n' begin, end = caller_match.span() self.blocks[blockIndex] = block[0:end] + extra_line + block[end:] return True # Done searching -- cgit v1.2.1