xpcom/analysis/static-init.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/analysis/static-init.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     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 +/**
     1.9 + * Detects static initializers i.e. functions called during static initialization.
    1.10 + */
    1.11 +
    1.12 +require({ after_gcc_pass: "cfg" });
    1.13 +
    1.14 +function process_tree(fn) {
    1.15 +  for each (let attr in translate_attributes(DECL_ATTRIBUTES(fn)))
    1.16 +    if (attr.name == "constructor")
    1.17 +      warning(pretty_func(fn) + " marked with constructor attribute\n");
    1.18 +
    1.19 +  if (decl_name_string(fn) != "__static_initialization_and_destruction_0")
    1.20 +    return;
    1.21 +
    1.22 +  let cfg = function_decl_cfg(fn);
    1.23 +  for (let isn in cfg_isn_iterator(cfg)) {
    1.24 +    if (isn.tree_code() != GIMPLE_CALL)
    1.25 +      continue;
    1.26 +    let decl = gimple_call_fndecl(isn);
    1.27 +    let lhs = gimple_call_lhs(isn);
    1.28 +    if (lhs) {
    1.29 +      warning(pretty_var(lhs) + " defined by call to " + pretty_func(decl) +
    1.30 +              " during static initialization", location_of(lhs));
    1.31 +    } else {
    1.32 +      let arg = constructorArg(isn);
    1.33 +      if (arg)
    1.34 +        warning(pretty_var(arg) + " defined by call to constructor " + pretty_func(decl) +
    1.35 +                " during static initialization", location_of(arg));
    1.36 +      else
    1.37 +        warning(pretty_func(decl) + " called during static initialization", location_of(decl));
    1.38 +    }
    1.39 +  }
    1.40 +}
    1.41 +
    1.42 +function constructorArg(call) {
    1.43 +  let decl = gimple_call_fndecl(call);
    1.44 +
    1.45 +  if (!DECL_CONSTRUCTOR_P(decl))
    1.46 +    return null;
    1.47 +
    1.48 +  let arg = gimple_call_arg_iterator(call).next();
    1.49 +  if (TYPE_MAIN_VARIANT(TREE_TYPE(TREE_TYPE(arg))) != DECL_CONTEXT(decl))
    1.50 +    throw new Error("malformed constructor call?!");
    1.51 +
    1.52 +  return arg.tree_code() == ADDR_EXPR ? TREE_OPERAND(arg, 0) : arg;
    1.53 +}
    1.54 +
    1.55 +function pretty_func(fn) {
    1.56 +  return rfunc_string(rectify_function_decl(fn));
    1.57 +}
    1.58 +
    1.59 +function pretty_var(v) {
    1.60 +  return type_string(TREE_TYPE(v)) + " " + expr_display(v);
    1.61 +}

mercurial