layout/analysis/simple-match.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/analysis/simple-match.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,43 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// This executes a very simple search for all functions that call a
     1.9 +// given set of functions. It's intended to be the simplest possible
    1.10 +// way of refactoring a common pattern of function calls. Of course,
    1.11 +// it's still up to a human to decide if the replacement is truely
    1.12 +// suitable, but this gets the low-hanging fruit.
    1.13 +
    1.14 +// Expects the variable 'patterns' to hold an object with replacement
    1.15 +// function names as keys and function lists as values. Any function
    1.16 +// in the tested source that calls all of the functions named in the
    1.17 +// list will be listed in the output as being likely candidates to
    1.18 +// instead call the replacement function.
    1.19 +
    1.20 +include("unstable/lazy_types.js");
    1.21 +
    1.22 +var matches = {};
    1.23 +
    1.24 +function identity(x) x;
    1.25 +
    1.26 +function process_cp_pre_genericize(fndecl)
    1.27 +{
    1.28 +    var c = [];
    1.29 +    function calls(t, stack)
    1.30 +    {
    1.31 +        try {
    1.32 +            t.tree_check(CALL_EXPR);
    1.33 +            var fn = callable_arg_function_decl(CALL_EXPR_FN(t));
    1.34 +            if (fn)
    1.35 +                c.push(decl_name_string(fn));
    1.36 +        }
    1.37 +        catch (e if e.TreeCheckError) { }
    1.38 +    }
    1.39 +
    1.40 +    walk_tree(DECL_SAVED_TREE(fndecl), calls);
    1.41 +
    1.42 +    for (let [fnreplace, pattern] in patterns)
    1.43 +        if (pattern.map(function(e){ return c.some(function(f) { return e == f; }); }).every(identity))
    1.44 +            if (fnreplace != (n = decl_name_string(fndecl)))
    1.45 +                print(fnreplace +" could probably be used in "+ n);
    1.46 +}

mercurial