enabling -Werror=assign-enum for kernel
Gleb Smirnoff <[email protected]>
| Newsgroups | gmane.os.freebsd.devel.hackers |
|---|---|
| Message-ID | <[email protected]> |
Hi,
modern clang has a nice feature: it allows to catch enum misuse. For example:
enum foo { A = 1, B } var;
var = 1; /* success */
var = A; /* success */
var = B; /* success */
var = 3; /* fail */
What about enums that are used as flags? For that it has
__attribute__((flag_enum)). This will allow using combination of values and
clearing them:
var = A | B; /* success */
var &= ~A; /* success */
var = 3; /* fail */
This seems like a nice enforcement of a good code. I already found a few
hamrless bugs with it and one in e1000 that could be a real bug. It also
highlights some sketchy code that better be refactored.
I have a branch where LINT is compilable with -Werror=assign-enum:
https://github.com/freebsd/freebsd-src/compare/main...glebius:FreeBSD:Werror%3Dassign-enum
There are basically three parts that require work:
1) The most common violators are SYSINIT(9)s that use (SI_SUB_FOO + 1) as
argument. I have posted a pack of reviews to cover that.
2) hwpmc has enum pmc_event that is generated by preprocessor and its use is
sketchy, thus files that utilize HWPMC_HOOKS are temporarily excluded from
the enforcements.
3) I was too tired to look at OFED, thus just disabled it in ${OFED_C} in
kern.mk
Comments are welcome!
--
Gleb Smirnoff