Chromium Code Reviews| Index: tools/check-name-clashes.py |
| diff --git a/tools/check-name-clashes.py b/tools/check-name-clashes.py |
| index e4489303270dbae94d66d075e9dc2dc9f5d759e9..e890773924175382a8ee20ea8e399cbb35f7fdbb 100755 |
| --- a/tools/check-name-clashes.py |
| +++ b/tools/check-name-clashes.py |
| @@ -8,135 +8,66 @@ import os |
| import re |
| import sys |
| -FILENAME = "src/runtime.cc" |
| -FUNCTION = re.compile("^RUNTIME_FUNCTION\(Runtime_(\w+)") |
| -FUNCTIONEND = "}\n" |
| -MACRO = re.compile(r"^#define ([^ ]+)\(([^)]*)\) *([^\\]*)\\?\n$") |
| -FIRST_WORD = re.compile("^\s*(.*?)[\s({\[]") |
| - |
| -# Expand these macros, they define further runtime functions. |
| -EXPAND_MACROS = [ |
| - "BUFFER_VIEW_GETTER", |
| - "DATA_VIEW_GETTER", |
| - "DATA_VIEW_SETTER", |
| - "ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION", |
| - "FIXED_TYPED_ARRAYS_CHECK_RUNTIME_FUNCTION", |
| - "RUNTIME_UNARY_MATH", |
| - "TYPED_ARRAYS_CHECK_RUNTIME_FUNCTION", |
| -] |
| +FILENAME = "src/runtime.h" |
| +LISTHEAD = re.compile("#define\s+(\w+LIST\w*)\((\w+)\)") |
| +LISTBODY = re.compile(".*\\\\$") |
|
Jakob Kummerow
2014/09/24 10:53:13
nit: if you use Python's r"..." notation, that'll
Yang
2014/09/24 11:52:17
Done.
The loop is written in a way to append the
|
| +BLACKLIST = ['INLINE_FUNCTION_LIST'] |
| class Function(object): |
| def __init__(self, match): |
| - self.name = match.group(1) |
| + self.name = match.group(1).strip() |
| -class Macro(object): |
| - def __init__(self, match): |
| - self.name = match.group(1) |
| - self.args = [s.strip() for s in match.group(2).split(",")] |
| - self.lines = [] |
| - self.indentation = 0 |
| - self.AddLine(match.group(3)) |
| - |
| - def AddLine(self, line): |
| - if not line: return |
| - if not self.lines: |
| - # This is the first line, detect indentation. |
| - self.indentation = len(line) - len(line.lstrip()) |
| - line = line.rstrip("\\\n ") |
| - if not line: return |
| - assert len(line[:self.indentation].strip()) == 0, \ |
| - ("expected whitespace: '%s', full line: '%s'" % |
| - (line[:self.indentation], line)) |
| - line = line[self.indentation:] |
| - if not line: return |
| - self.lines.append(line + "\n") |
| - |
| - def Finalize(self): |
| - for arg in self.args: |
| - pattern = re.compile(r"(##|\b)%s(##|\b)" % arg) |
| - for i in range(len(self.lines)): |
| - self.lines[i] = re.sub(pattern, "%%(%s)s" % arg, self.lines[i]) |
| - |
| - def FillIn(self, arg_values): |
| - filler = {} |
| - assert len(arg_values) == len(self.args) |
| - for i in range(len(self.args)): |
| - filler[self.args[i]] = arg_values[i] |
| - result = [] |
| - for line in self.lines: |
| - result.append(line % filler) |
| - return result |
| - |
| - |
| -def ReadFileAndExpandMacros(filename): |
| - found_macros = {} |
| +def ListName(list): |
| + return LISTHEAD.match(list[0]).group(1) |
| + |
| + |
| +def ListMacroRe(list): |
| + macro = LISTHEAD.match(list[0]).group(2) |
| + re_string = "\s*%s\((\w+)" % macro |
| + return re.compile(re_string) |
| + |
| + |
| +def ReadFile(filename): |
| expanded_lines = [] |
| with open(filename, "r") as f: |
| - found_macro = None |
| for line in f: |
| - if found_macro is not None: |
| - found_macro.AddLine(line) |
| - if not line.endswith("\\\n"): |
| - found_macro.Finalize() |
| - found_macro = None |
| - continue |
| + expanded_lines.append(line) |
| + return expanded_lines |
| - match = MACRO.match(line) |
| - if match: |
| - found_macro = Macro(match) |
| - if found_macro.name in EXPAND_MACROS: |
| - found_macros[found_macro.name] = found_macro |
| - else: |
| - found_macro = None |
| - continue |
| - match = FIRST_WORD.match(line) |
| +def FindLists(lines): |
| + lists = [] |
| + current_list = [] |
| + for line in lines: |
|
Jakob Kummerow
2014/09/24 10:53:13
I'd inline ReadFile here and iterate over the open
Yang
2014/09/24 11:52:17
Done.
|
| + if len(current_list) == 0: |
| + match = LISTHEAD.match(line) |
| if match: |
| - first_word = match.group(1) |
| - if first_word in found_macros: |
| - MACRO_CALL = re.compile("%s\(([^)]*)\)" % first_word) |
| - match = MACRO_CALL.match(line) |
| - assert match |
| - args = [s.strip() for s in match.group(1).split(",")] |
| - expanded_lines += found_macros[first_word].FillIn(args) |
| - continue |
| - |
| - expanded_lines.append(line) |
| - return expanded_lines |
| + current_list.append(line) |
| + else: |
| + current_list.append(line) |
| + match = LISTBODY.match(line) |
| + if not match: |
| + lists.append(current_list) |
| + current_list = [] |
| + return lists |
| # Detects runtime functions by parsing FILENAME. |
| def FindRuntimeFunctions(): |
| functions = [] |
| - expanded_lines = ReadFileAndExpandMacros(FILENAME) |
| - function = None |
| - partial_line = "" |
| - for line in expanded_lines: |
| - # Multi-line definition support, ignoring macros. |
| - if line.startswith("RUNTIME_FUNCTION") and not line.endswith("{\n"): |
| - if line.endswith("\\\n"): continue |
| - partial_line = line.rstrip() |
| + lines = ReadFile(FILENAME) |
| + lists = FindLists(lines) |
| + for list in lists: |
| + if ListName(list) in BLACKLIST: |
|
Jakob Kummerow
2014/09/24 10:53:13
I'd be inclined to move this logic into the first
Yang
2014/09/24 11:52:17
Done.
|
| continue |
| - if partial_line: |
| - partial_line += " " + line.strip() |
| - if partial_line.endswith("{"): |
| - line = partial_line |
| - partial_line = "" |
| - else: |
| - continue |
| - |
| - match = FUNCTION.match(line) |
| - if match: |
| - function = Function(match) |
| - continue |
| - if function is None: continue |
| - |
| - if line == FUNCTIONEND: |
| - if function is not None: |
| - functions.append(function) |
| - function = None |
| + function_re = ListMacroRe(list) |
| + for line in list: |
| + match = function_re.match(line) |
| + if match: |
| + functions.append(Function(match)) |
| + print(match.group(1)) |
|
Jakob Kummerow
2014/09/24 10:53:13
debugging leftover?
Yang
2014/09/24 11:52:17
Done.
|
| return functions |