docs/misra/rules.rst | 3 +++ xen/include/xen/perfc.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-)
Using enumerators declared in a named enum, such as the one modified,
as operands to arithmetic operators is not allowed by MISRA C:2012 Rule 10.1.
The enumerators of an anonymous enum can be used instead.
Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
---
This violation manifeststs itself, for instance, in all uses of macro
'perfc_incra' from xen/include/xen/perfc.h, because the expansion
contains an arithmetic operation on two enum constants from enum perfcounter.
( (*nr) <= PERFC_LAST_hypercalls - PERFC_hypercalls ? [...]
---
docs/misra/rules.rst | 3 +++
xen/include/xen/perfc.h | 2 +-
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/docs/misra/rules.rst b/docs/misra/rules.rst
index 3139ca7ae6dd..26c3ff819948 100644
--- a/docs/misra/rules.rst
+++ b/docs/misra/rules.rst
@@ -341,6 +341,9 @@ maintainers if you want to suggest a change.
compilers' extensions)
- Implicit conversions to boolean for conditionals (?: if while
for) and logical operators (! || &&)
+ - The essential type model allows the constants defined by anonymous
+ enums (e.g., enum { A, B, C }) to be used as operands to arithmetic
+ operators, as they have a signed essential type.
* - `Rule 10.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_02.c>`_
- Required
diff --git a/xen/include/xen/perfc.h b/xen/include/xen/perfc.h
index 7c5ce537bd02..96022c07481e 100644
--- a/xen/include/xen/perfc.h
+++ b/xen/include/xen/perfc.h
@@ -39,7 +39,7 @@
#define PERFSTATUS PERFCOUNTER
#define PERFSTATUS_ARRAY PERFCOUNTER_ARRAY
-enum perfcounter {
+enum {
#include <xen/perfc_defn.h>
NUM_PERFCOUNTERS
};
--
2.34.1
On Wed, 11 Oct 2023, Nicola Vetrini wrote:
> Using enumerators declared in a named enum, such as the one modified,
> as operands to arithmetic operators is not allowed by MISRA C:2012 Rule 10.1.
> The enumerators of an anonymous enum can be used instead.
>
> Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
> ---
> This violation manifeststs itself, for instance, in all uses of macro
> 'perfc_incra' from xen/include/xen/perfc.h, because the expansion
> contains an arithmetic operation on two enum constants from enum perfcounter.
>
> ( (*nr) <= PERFC_LAST_hypercalls - PERFC_hypercalls ? [...]
>
> ---
> docs/misra/rules.rst | 3 +++
> xen/include/xen/perfc.h | 2 +-
> 2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/docs/misra/rules.rst b/docs/misra/rules.rst
> index 3139ca7ae6dd..26c3ff819948 100644
> --- a/docs/misra/rules.rst
> +++ b/docs/misra/rules.rst
> @@ -341,6 +341,9 @@ maintainers if you want to suggest a change.
> compilers' extensions)
> - Implicit conversions to boolean for conditionals (?: if while
> for) and logical operators (! || &&)
> + - The essential type model allows the constants defined by anonymous
> + enums (e.g., enum { A, B, C }) to be used as operands to arithmetic
> + operators, as they have a signed essential type.
>
> * - `Rule 10.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_02.c>`_
> - Required
> diff --git a/xen/include/xen/perfc.h b/xen/include/xen/perfc.h
> index 7c5ce537bd02..96022c07481e 100644
> --- a/xen/include/xen/perfc.h
> +++ b/xen/include/xen/perfc.h
> @@ -39,7 +39,7 @@
> #define PERFSTATUS PERFCOUNTER
> #define PERFSTATUS_ARRAY PERFCOUNTER_ARRAY
>
> -enum perfcounter {
> +enum {
> #include <xen/perfc_defn.h>
> NUM_PERFCOUNTERS
> };
> --
> 2.34.1
>
On 11/10/2023 15:03, Nicola Vetrini wrote:
> Using enumerators declared in a named enum, such as the one modified,
> as operands to arithmetic operators is not allowed by MISRA C:2012 Rule
> 10.1.
> The enumerators of an anonymous enum can be used instead.
>
> Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
> ---
> This violation manifeststs itself, for instance, in all uses of macro
> 'perfc_incra' from xen/include/xen/perfc.h, because the expansion
> contains an arithmetic operation on two enum constants from enum
> perfcounter.
>
> ( (*nr) <= PERFC_LAST_hypercalls - PERFC_hypercalls ? [...]
>
> ---
> docs/misra/rules.rst | 3 +++
> xen/include/xen/perfc.h | 2 +-
> 2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/docs/misra/rules.rst b/docs/misra/rules.rst
> index 3139ca7ae6dd..26c3ff819948 100644
> --- a/docs/misra/rules.rst
> +++ b/docs/misra/rules.rst
> @@ -341,6 +341,9 @@ maintainers if you want to suggest a change.
> compilers' extensions)
> - Implicit conversions to boolean for conditionals (?: if
> while
> for) and logical operators (! || &&)
> + - The essential type model allows the constants defined by
> anonymous
> + enums (e.g., enum { A, B, C }) to be used as operands to
> arithmetic
> + operators, as they have a signed essential type.
>
> * - `Rule 10.2
> <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_02.c>`_
> - Required
> diff --git a/xen/include/xen/perfc.h b/xen/include/xen/perfc.h
> index 7c5ce537bd02..96022c07481e 100644
> --- a/xen/include/xen/perfc.h
> +++ b/xen/include/xen/perfc.h
> @@ -39,7 +39,7 @@
> #define PERFSTATUS PERFCOUNTER
> #define PERFSTATUS_ARRAY PERFCOUNTER_ARRAY
>
> -enum perfcounter {
> +enum {
> #include <xen/perfc_defn.h>
> NUM_PERFCOUNTERS
> };
> --
> 2.34.1
See [1] for a discussion on the possible alternatives to this approach.
[1] https://marc.info/?l=xen-devel&m=169658364229813
--
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
© 2016 - 2026 Red Hat, Inc.