layout/analysis/simple-match.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:530df9bdbdb9
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 // This executes a very simple search for all functions that call a
6 // given set of functions. It's intended to be the simplest possible
7 // way of refactoring a common pattern of function calls. Of course,
8 // it's still up to a human to decide if the replacement is truely
9 // suitable, but this gets the low-hanging fruit.
10
11 // Expects the variable 'patterns' to hold an object with replacement
12 // function names as keys and function lists as values. Any function
13 // in the tested source that calls all of the functions named in the
14 // list will be listed in the output as being likely candidates to
15 // instead call the replacement function.
16
17 include("unstable/lazy_types.js");
18
19 var matches = {};
20
21 function identity(x) x;
22
23 function process_cp_pre_genericize(fndecl)
24 {
25 var c = [];
26 function calls(t, stack)
27 {
28 try {
29 t.tree_check(CALL_EXPR);
30 var fn = callable_arg_function_decl(CALL_EXPR_FN(t));
31 if (fn)
32 c.push(decl_name_string(fn));
33 }
34 catch (e if e.TreeCheckError) { }
35 }
36
37 walk_tree(DECL_SAVED_TREE(fndecl), calls);
38
39 for (let [fnreplace, pattern] in patterns)
40 if (pattern.map(function(e){ return c.some(function(f) { return e == f; }); }).every(identity))
41 if (fnreplace != (n = decl_name_string(fndecl)))
42 print(fnreplace +" could probably be used in "+ n);
43 }

mercurial