blob: 89a7dee7a163cc1ca70c28f71b7d0b409cb97b13 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001#!/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
6import js2c
7import os
8import re
9import sys
10
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011FILENAME = "src/runtime/runtime.h"
12LISTHEAD = re.compile(r"#define\s+(\w+LIST\w*)\((\w+)\)")
13LISTBODY = re.compile(r".*\\$")
14BLACKLIST = ['INLINE_FUNCTION_LIST']
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015
16
17class Function(object):
18 def __init__(self, match):
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019 self.name = match.group(1).strip()
20
21def ListMacroRe(list):
22 macro = LISTHEAD.match(list[0]).group(2)
23 re_string = "\s*%s\((\w+)" % macro
24 return re.compile(re_string)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025
26
Emily Bernierd0a1eb72015-03-24 16:35:39 -040027def FindLists(filename):
28 lists = []
29 current_list = []
30 mode = "SEARCHING"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 with open(filename, "r") as f:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 for line in f:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040033 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046
47
48# Detects runtime functions by parsing FILENAME.
49def FindRuntimeFunctions():
50 functions = []
Emily Bernierd0a1eb72015-03-24 16:35:39 -040051 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))
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 return functions
59
60
61class Builtin(object):
62 def __init__(self, match):
63 self.name = match.group(1)
64
65
66def 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
99def 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
118if __name__ == "__main__":
119 sys.exit(Main())