This is an archive of the discontinued LLVM Phabricator instance.

switch on enum should be exhaustive and warning-free
ClosedPublic

Authored by penryu on Jul 5 2017, 3:28 PM.

Details

Summary

Testing the value of type_code against the closed enum TypeCodes
provides statically verifiable completeness of testing. However, one
branch assigns to type_code by casting directly from a masked integer
value. This is currently handled by adding a default: case after
checking each TypeCodes instance. This patch introduces a bool variable
containing the "default" state value, allowing the switch to be
exhaustive, protect against future instances not being handled in the
switch, and preserves the original logic.

This addresses the warning:
warning: default label in switch which covers all enumeration values
[-Wcovered-switch-default]

As an issue of maintainability, the bitmask on line 524 handles the
current values of TypeCodes enum, but this will be invalid if the enum
is extended. This patch does not address this, and a more closed
conversion from cfinfoa -> TypeCodes would help protect against this.

Event Timeline

penryu created this revision.Jul 5 2017, 3:28 PM
penryu edited the summary of this revision. (Show Details)Jul 5 2017, 3:37 PM
sas accepted this revision.Jul 5 2017, 10:09 PM
sas added a subscriber: sas.

Looks like the numeric values of TypeCode are contiguous. It would probably be cleaner to a check after the cast to validate the value instead of having to add a line for each label in the switch statement. Something like this:

if (type_code < TypeCodes::sint8 || type_code > TypeCodes::sint128)
    return false;

You could also add TypeCodesBegin = -1 and TypeCodesEnd as the first and last enumerations of TypeCodes so the above check would not have to be modified when you change the definition of TypeCodes.

Other than that, looks good.

This revision is now accepted and ready to land.Jul 5 2017, 10:09 PM

Thanks, sas.

I'll be honest, my prefer solution involves an inlined function (uint64_t -> TypeCodes) that eliminates the cast from the NSNumberSummaryProvider() method altogether. This way we can handle any dirty mappings from raw memory directory to the enum within the one function, and take advantage of the compiler's exhaustiveness check to ensure we're covering our bases.

I'll ponder a bit and consider reworking this patch in that form.

penryu planned changes to this revision.Jul 5 2017, 10:32 PM
sas added a comment.Jul 5 2017, 10:33 PM

Putting that check in an inline function instead of doing ad-hoc validation in the call-site seems better indeed.

penryu requested review of this revision.Jul 10 2017, 5:20 PM

I need to learn more about the number formatters before I make more significant changes than those below. As written currently, it leaves the logic intact and resolves the warning. One step closer to warning-free LLDB!

This revision is now accepted and ready to land.Jul 10 2017, 5:20 PM
penryu closed this revision.Jul 11 2017, 2:06 PM