diff mbox series

[XEN,for-4.19] xen/include: make enum perfcounter anonymous

Message ID d739fefde6f142cec10899ed2c5eb81f91618bf0.1697028983.git.nicola.vetrini@bugseng.com (mailing list archive)
State New, archived
Headers show
Series [XEN,for-4.19] xen/include: make enum perfcounter anonymous | expand

Commit Message

Nicola Vetrini Oct. 11, 2023, 1:03 p.m. UTC
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(-)

--
2.34.1

Comments

Nicola Vetrini Oct. 11, 2023, 1:06 p.m. UTC | #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
Stefano Stabellini Oct. 12, 2023, 11:18 p.m. UTC | #2
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
>
diff mbox series

Patch

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
 };