check-name-clashes.py 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
# Copyright 2014 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import js2c
import os
import re
import sys

11
FILENAME = "src/runtime/runtime.h"
12 13 14
LISTHEAD = re.compile(r"#define\s+(\w+LIST\w*)\((\w+)\)")
LISTBODY = re.compile(r".*\\$")
BLACKLIST = ['INLINE_FUNCTION_LIST']
15 16 17 18


class Function(object):
  def __init__(self, match):
19
    self.name = match.group(1).strip()
20

21 22 23 24
def ListMacroRe(list):
  macro = LISTHEAD.match(list[0]).group(2)
  re_string = "\s*%s\((\w+)" % macro
  return re.compile(re_string)
25 26


27 28 29 30
def FindLists(filename):
  lists = []
  current_list = []
  mode = "SEARCHING"
31 32
  with open(filename, "r") as f:
    for line in f:
33 34 35 36 37 38 39 40 41 42 43 44 45
      if mode == "SEARCHING":
        match = LISTHEAD.match(line)
        if match and match.group(1) not in BLACKLIST:
          mode = "APPENDING"
          current_list.append(line)
      else:
        current_list.append(line)
        match = LISTBODY.match(line)
        if not match:
          mode = "SEARCHING"
          lists.append(current_list)
          current_list = []
  return lists
46 47 48 49 50


# Detects runtime functions by parsing FILENAME.
def FindRuntimeFunctions():
  functions = []
51 52 53 54 55 56 57
  lists = FindLists(FILENAME)
  for list in lists:
    function_re = ListMacroRe(list)
    for line in list:
      match = function_re.match(line)
      if match:
        functions.append(Function(match))
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  return functions


class Builtin(object):
  def __init__(self, match):
    self.name = match.group(1)


def FindJSNatives():
  PATH = "src"
  fileslist = []
  for (root, dirs, files) in os.walk(PATH):
    for f in files:
      if f.endswith(".js"):
        fileslist.append(os.path.join(root, f))
  natives = []
  regexp = re.compile("^function (\w+)\s*\((.*?)\) {")
  matches = 0
  for filename in fileslist:
    with open(filename, "r") as f:
      file_contents = f.read()
    file_contents = js2c.ExpandInlineMacros(file_contents)
    lines = file_contents.split("\n")
    partial_line = ""
    for line in lines:
      if line.startswith("function") and not '{' in line:
        partial_line += line.rstrip()
        continue
      if partial_line:
        partial_line += " " + line.strip()
        if '{' in line:
          line = partial_line
          partial_line = ""
        else:
          continue
      match = regexp.match(line)
      if match:
        natives.append(Builtin(match))
  return natives


def Main():
  functions = FindRuntimeFunctions()
  natives = FindJSNatives()
  errors = 0
  runtime_map = {}
  for f in functions:
    runtime_map[f.name] = 1
  for b in natives:
    if b.name in runtime_map:
      print("JS_Native/Runtime_Function name clash: %s" % b.name)
      errors += 1

  if errors > 0:
    return 1
  print("Runtime/Natives name clashes: checked %d/%d functions, all good." %
        (len(functions), len(natives)))
  return 0


if __name__ == "__main__":
  sys.exit(Main())