Rule 10.1 violations in perfc_incra and PERFCOUNTER_ARRAY
Given the following macros in <xen/perfc.h> and
#define perfc_incra(x,y) \
( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ?
\
++this_cpu(perfcounters)[PERFC_ ## x + (y)] : 0 )and the following violation
xen/arch/arm/traps.c:1407.5-1407.32:
reference to enum constant PERFC_hypercalls' has essential type enum
perfcounter'
and standard type `int'
<preprocessed xen/arch/arm/traps.c>:11606.29-11606.44: preprocessed
tokens
<scratch space>:137.1-137.16: expanded from macro PERFC_' xen/include/xen/perfc.h:69.35-69.45: expanded from macro perfc_incra'
xen/arch/arm/traps.c:1407.5-1407.32:
`+' addition operator expects a number or a character
<preprocessed xen/arch/arm/traps.c>:11606.46: preprocessed tokens xen/include/xen/perfc.h:69.47: expanded from macro `perfc_incra'
the difference between enumerated values is forbidden by the Rule. In the coding standard's interpretation, named enums are unordered list of symbols, which can only be compared for equality.
There are a few possible paths forward:
1. use means different from named enums to generate these constants (such as #define-s or constants integers);
2. explicitly deviate subtraction of enums, therefore defining an explicit ordering on enumerated values;
3. use an unnamed enum, effectively considering the enumerated values as plain integers. This does not violate the Rule.
Similarly the sum in
#define PERFCOUNTER_ARRAY( name, descr, size ) \
PERFC_##name, \
PERFC_LAST_##name = PERFC_ ## name + (size) - sizeof(char[2 * !!(size)
- 1]),
causes other analogous violations.
Lastly, this is a concern that manifested only in ARM code, but does not seem to be related to ARM-specific files, so there is perhaps some difference in the configuration of the two analyzed builds.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Quoted 27 lines · Nicola Vetrini
Given the following macros in <
xen/perfc.h> and#define perfc_incra(x,y) \ ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ? \ ++this_cpu(perfcounters)[PERFC_ ## x + (y)] : 0 )and the following violation
xen/arch/arm/traps.c:1407.5-1407.32:
reference to enum constantPERFC_hypercalls' has essential typeenum
perfcounter'
and standard type `int'
<preprocessed xen/arch/arm/traps.c>:11606.29-11606.44: preprocessed tokens <scratch space>:137.1-137.16: expanded from macroPERFC_' xen/include/xen/perfc.h:69.35-69.45: expanded from macroperfc_incra'
xen/arch/arm/traps.c:1407.5-1407.32:
`+' addition operator expects a number or a character
<preprocessed xen/arch/arm/traps.c>:11606.46: preprocessed tokens xen/include/xen/perfc.h:69.47: expanded from macro `perfc_incra'the difference between enumerated values is forbidden by the Rule. In the coding standard's
interpretation, named enums are unordered list of symbols, which can only be compared for
equality.
There are a few possible paths forward:
Quoted 3 lines
1. use means different from named enums to generate these constants (such as
#define-s or constants integers);
This is a viable option
Quoted 3 lines
2. explicitly deviate subtraction of enums, therefore defining an explicit ordering on
enumerated values;
I would prefer to avoid a project-wide deviation, because I think in general it is a good rule to have. If we go with a deviation, I think it would be better to deviate PERFCOUNTER or perfc.h specifically. This is a file that changes very rarely. We could make the case that this is safe with GCC (most probably is) and this case was reviewed carefully and doesn't change often (3 changes in the last 10 yeas).
Quoted 3 lines
3. use an unnamed enum, effectively considering the enumerated values as plain integers.
This does not violate the Rule.
What do you mean by unname enum?
Quoted 52 lines · Stefano Stabellini
Quoted 32 lines · Nicola Vetrini
Given the following macros in <
xen/perfc.h> and#define
perfc_incra(x,y)\ ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ? \ ++this_cpu(perfcounters)[PERFC_ ## x + (y)] : 0 )and the following violation
xen/arch/arm/traps.c:1407.5-1407.32:
reference to enum constant `PERFC_hypercalls' has essential type
`enum
perfcounter'
and standard type `int'
<preprocessed xen/arch/arm/traps.c>:11606.29-11606.44: preprocessed
tokens
<scratch space>:137.1-137.16: expanded from macro `PERFC_' xen/include/xen/perfc.h:69.35-69.45: expanded from macro
`perfc_incra'
xen/arch/arm/traps.c:1407.5-1407.32:
`+' addition operator expects a number or a character
<preprocessed xen/arch/arm/traps.c>:11606.46: preprocessed tokens xen/include/xen/perfc.h:69.47: expanded from macro `perfc_incra'the difference between enumerated values is forbidden by the Rule. In the coding standard's
interpretation, named enums are unordered list of symbols, which can only be compared for
equality.
There are a few possible paths forward:Quoted 3 lines
1. use means different from named enums to generate these constants (such as
#define-s or constants integers);This is a viable option
Quoted 3 lines
2. explicitly deviate subtraction of enums, therefore defining an explicit ordering on
enumerated values;I would prefer to avoid a project-wide deviation, because I think in general it is a good rule to have. If we go with a deviation, I think it would be better to deviate PERFCOUNTER or
perfc.hspecifically. This is a file that changes very rarely. We could make the case that this is safe with GCC (most probably is) and this case was reviewed carefully and doesn't change often (3 changes in the last 10 yeas).
Quoted 5 lines
Quoted 3 lines
3. use an unnamed enum, effectively considering the enumerated values as plain integers.
This does not violate the Rule.What do you mean by unname enum?
e.g.
enum {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};
instead of
enum perfcounter {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Quoted 65 lines · Nicola Vetrini
Quoted 50 lines · Stefano Stabellini
Quoted 29 lines · Nicola Vetrini
Given the following macros in <
xen/perfc.h> and#define perfc_incra(x,y) \ ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ? \ ++this_cpu(perfcounters)[PERFC_ ## x + (y)] : 0 )and the following violation
xen/arch/arm/traps.c:1407.5-1407.32:
reference to enum constantPERFC_hypercalls' has essential typeenum
perfcounter'
and standard type `int'
<preprocessed xen/arch/arm/traps.c>:11606.29-11606.44: preprocessed
tokens
<scratch space>:137.1-137.16: expanded from macroPERFC_' xen/include/xen/perfc.h:69.35-69.45: expanded from macroperfc_incra'
xen/arch/arm/traps.c:1407.5-1407.32:
`+' addition operator expects a number or a character
<preprocessed xen/arch/arm/traps.c>:11606.46: preprocessed tokens xen/include/xen/perfc.h:69.47: expanded from macro `perfc_incra'the difference between enumerated values is forbidden by the Rule. In the coding standard's
interpretation, named enums are unordered list of symbols, which can only be
compared for
equality.
There are a few possible paths forward:Quoted 4 lines
1. use means different from named enums to generate these constants (such as
#define-s or constants integers);This is a viable option
Quoted 3 lines
2. explicitly deviate subtraction of enums, therefore defining an explicit ordering on
enumerated values;I would prefer to avoid a project-wide deviation, because I think in general it is a good rule to have. If we go with a deviation, I think it would be better to deviate PERFCOUNTER or
perfc.hspecifically. This is a file that changes very rarely. We could make the case that this is safe with GCC (most probably is) and this case was reviewed carefully and doesn't change often (3 changes in the last 10 yeas).Quoted 6 lines
Quoted 4 lines
3. use an unnamed enum, effectively considering the enumerated values as plain
integers.
This does not violate the Rule.What do you mean by unname enum?
e.g.
enum {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};
Quoted 6 lines
instead of
enum perfcounter {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};
I think this should be easy to do in this case. I gave it a quick try and it seems to still build successfully. It could be the best way forward for this instance.
However in general I am confused why unnamed enum can do comparisons between members and named enums cannot. What is the reason? In any case, I think we should clarify this detail in the notes section of docs/misra/rules.rst, because I don't think it was clear to anyone that there is a difference in behavior between named and unnamed enums.
Quoted 28 lines
Quoted 15 lines
Quoted 7 lines
Quoted 5 lines
3. use an unnamed enum, effectively considering the enumerated values as plain
integers.
This does not violate the Rule.What do you mean by unname enum?
e.g.
enum {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};Quoted 6 lines
instead of
enum perfcounter {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};I think this should be easy to do in this case. I gave it a quick try and it seems to still build successfully. It could be the best way forward for this instance.
Quoted 5 lines
However in general I am confused why unnamed enum can do comparisons between members and named enums cannot. What is the reason? In any case, I think we should clarify this detail in the notes section of
docs/misra/rules.rst, because I don't think it was clear to anyone that there is a difference in behavior between named and unnamed enums.
MISRA regards unnamed enums as nothing more than bags of constants that may or may not be related. Such enumeration constants always have a signed essential type and also the anonymous enum type cannot be used, for instance, in the definition of new types, as opposed to a named "enum x {}" that introduces an essential type "enum<x>". I'll send a patch and document the difference.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)