[LLVMbugs] [Bug 9144] New: The static analyzer should warn when +initialize doesn't call [super initialize]

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Feb 4 16:52:45 PST 2011


           Summary: The static analyzer should warn when +initialize
                    doesn't call [super initialize]
           Product: clang
           Version: unspecified
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
        AssignedTo: kremenek at apple.com
        ReportedBy: kc at omnigroup.com
                CC: llvmbugs at cs.uiuc.edu


    The static analyzer should warn when +initialize doesn't call [super
initialize], since that can lead to inconsistent and unreliable behavior with
respect to initializing a class.


    One should always call [super initialize] because that's what happens if
you don't implement +initialize--and you shouldn't change a subclass' behavior
when you implement a subclassed method.

    In Objective C, a class' +initialize is supposed to be called for itself
and for every subclass which is initialized.  This is the implemented and
documented behavior of the runtime.

    This is an important feature of the language:  the superclass may need to
register some information for each of its subclasses, e.g. to set up
class-specific mapping dictionaries, or to register CoreData change
notifications for dependent keys.   The superclass doesn't and shouldn't know
which subclasses are non-implementing and the subclasses shouldn't need to know
what initialization the superclass needs to do, the superclass should be able
to do class-specific initialization for each of its subclasses.

    I can think of no benefit to not calling [super initialize] except to avoid
some slight overhead--the same benefit one might achieve by not calling [super
init] (which also seems like terrible general advice).  And there would be a
big downside in terms of consistency of behavior between classes which do or
don't implement +initialize and consistency of implementation between the way
you might implement +initialize and the way you would implement other methods.

    Instead, as a general rule, one should know that any time they subclass any
method they should call the superclass' implementation (unless they are
intentionally trying to bypass/replace the logic provided by the superclass). 
This is just as true for +initialize as it is for any other method.

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list