Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / tools / check-name-clashes.py
1 #!/usr/bin/env python
2 # Copyright 2014 the V8 project authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import js2c
7 import os
8 import re
9 import sys
10
11 FILENAME = "src/runtime/runtime.h"
12 LISTHEAD = re.compile(r"#define\s+(\w+LIST\w*)\((\w+)\)")
13 LISTBODY = re.compile(r".*\\$")
14 BLACKLIST = ['INLINE_FUNCTION_LIST']
15
16
17 class Function(object):
18   def __init__(self, match):
19     self.name = match.group(1).strip()
20
21 def ListMacroRe(list):
22   macro = LISTHEAD.match(list[0]).group(2)
23   re_string = "\s*%s\((\w+)" % macro
24   return re.compile(re_string)
25
26
27 def FindLists(filename):
28   lists = []
29   current_list = []
30   mode = "SEARCHING"
31   with open(filename, "r") as f:
32     for line in f:
33       if mode == "SEARCHING":
34         match = LISTHEAD.match(line)
35         if match and match.group(1) not in BLACKLIST:
36           mode = "APPENDING"
37           current_list.append(line)
38       else:
39         current_list.append(line)
40         match = LISTBODY.match(line)
41         if not match:
42           mode = "SEARCHING"
43           lists.append(current_list)
44           current_list = []
45   return lists
46
47
48 # Detects runtime functions by parsing FILENAME.
49 def FindRuntimeFunctions():
50   functions = []
51   lists = FindLists(FILENAME)
52   for list in lists:
53     function_re = ListMacroRe(list)
54     for line in list:
55       match = function_re.match(line)
56       if match:
57         functions.append(Function(match))
58   return functions
59
60
61 class Builtin(object):
62   def __init__(self, match):
63     self.name = match.group(1)
64
65
66 def FindJSNatives():
67   PATH = "src"
68   fileslist = []
69   for (root, dirs, files) in os.walk(PATH):
70     for f in files:
71       if f.endswith(".js"):
72         fileslist.append(os.path.join(root, f))
73   natives = []
74   regexp = re.compile("^function (\w+)\s*\((.*?)\) {")
75   matches = 0
76   for filename in fileslist:
77     with open(filename, "r") as f:
78       file_contents = f.read()
79     file_contents = js2c.ExpandInlineMacros(file_contents)
80     lines = file_contents.split("\n")
81     partial_line = ""
82     for line in lines:
83       if line.startswith("function") and not '{' in line:
84         partial_line += line.rstrip()
85         continue
86       if partial_line:
87         partial_line += " " + line.strip()
88         if '{' in line:
89           line = partial_line
90           partial_line = ""
91         else:
92           continue
93       match = regexp.match(line)
94       if match:
95         natives.append(Builtin(match))
96   return natives
97
98
99 def Main():
100   functions = FindRuntimeFunctions()
101   natives = FindJSNatives()
102   errors = 0
103   runtime_map = {}
104   for f in functions:
105     runtime_map[f.name] = 1
106   for b in natives:
107     if b.name in runtime_map:
108       print("JS_Native/Runtime_Function name clash: %s" % b.name)
109       errors += 1
110
111   if errors > 0:
112     return 1
113   print("Runtime/Natives name clashes: checked %d/%d functions, all good." %
114         (len(functions), len(natives)))
115   return 0
116
117
118 if __name__ == "__main__":
119   sys.exit(Main())