xpcom/analysis/must-override.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/analysis/must-override.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,52 @@
     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 + * Detect classes that should have overridden members of their parent
    1.10 + * classes but didn't.
    1.11 + *
    1.12 + * Example:
    1.13 + *
    1.14 + * struct S {
    1.15 + *   virtual NS_MUST_OVERRIDE void f();
    1.16 + *   virtual void g();
    1.17 + * };
    1.18 + *
    1.19 + * struct A : S { virtual void f(); }; // ok
    1.20 + * struct B : S { virtual NS_MUST_OVERRIDE void f(); }; // also ok
    1.21 + *
    1.22 + * struct C : S { virtual void g(); }; // ERROR: must override f()
    1.23 + * struct D : S { virtual void f(int); }; // ERROR: different overload
    1.24 + * struct E : A { }; // ok: A's definition of f() is good for subclasses
    1.25 + * struct F : B { }; // ERROR: B's definition of f() is still must-override
    1.26 + *
    1.27 + * We don't care if you define the method or not.
    1.28 + */
    1.29 +
    1.30 +function get_must_overrides(cls)
    1.31 +{
    1.32 +  let mos = {};
    1.33 +  for each (let base in cls.bases)
    1.34 +    for each (let m in base.type.members)
    1.35 +      if (hasAttribute(m, 'NS_must_override')) 
    1.36 +       mos[m.shortName] = m;
    1.37 +
    1.38 +  return mos;
    1.39 +}
    1.40 +
    1.41 +function process_type(t)
    1.42 +{
    1.43 +  if (t.isIncomplete || (t.kind != 'class' && t.kind != 'struct'))
    1.44 +    return;
    1.45 +
    1.46 +  let mos = get_must_overrides(t);
    1.47 +  for each (let m in t.members) {
    1.48 +    let mos_m = mos[m.shortName]
    1.49 +    if (mos_m && signaturesMatch(mos_m, m))
    1.50 +      delete mos[m.shortName];
    1.51 +  }
    1.52 +
    1.53 +  for each (let u in mos)
    1.54 +    error(t.kind + " " + t.name + " must override " + u.name, t.loc);
    1.55 +}

mercurial