如何使(1 LT;< 9)通过MISRA? [英] How to make (1 << 9) pass MISRA?

查看:292
本文介绍了如何使(1 LT;< 9)通过MISRA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Parasoft的静态分析与MISRA C 2004检查开启。

该软件是嵌入式系统。我们喜欢来形容常量,如下所示:

  [1]的#define MOTOR_ON(1 LT;< 9)

这将表明,在寄存器中的第9位应该是1转动电机上。

这位前pression失败MISRA,所以我们改变了它:

  [2]的#define MOTOR_ON(1U<< 9U)

的变化转换为无符号整型常量,因为移动是最好的无符号整数实现。

在声明2的前pression,还是因为右手操作者(9U)的失败需要检查。根据MISRA,如果右手操作者比基础类型左手操作者的比特宽度大时,有一个问题。

问题的基础是1U有一个基本类型 unsigned char型或8位。结果
我们正在写寄存器是16位,所以理论上没有问题。

我怎样才能改变[2],使其通过MISRA C 2004年,preferring不使用注塑前pression?

我使用的IAR Embedded Workbench与8/32位模式下的ARM7TDMI处理器。

修改1:例code

 无效turn_on_motor(无效);
#定义MOTOR_ON(1U<< 9U)
无效turn_on_motor(无效)
{
    uint16_t * const的p_motor_control =(uint16_t *)(0x01234567U);
    * p_motor_control = MOTOR_ON;
}

错误文本:恒作为移位运算符的右侧操作数应限制

从Parasoft公司提供的MISRA规则文档:

 规则报告违规,如果: - 右侧操作数是负值或数值的常量,
  超过了左边的操作数的长度(位) - 右侧操作数不是恒定的,而不是由具体的检查
  模式


解决方案

您也可以简单地通过规避转移问题

 的#define MOTOR_ON((uint16_t)512U)/ * 1 LT;< 9 = 2 ^ 9 * /

We are using Parasoft Static Analysis with MISRA C 2004 checker turned on.

The software is an embedded system. We like to describe constants as follows:

[1]    #define MOTOR_ON (1 << 9)  

This would show that the 9th bit in the register should be a 1 to turn on the motor.

The expression is failing MISRA, so we changed it:

[2]    #define MOTOR_ON (1U << 9U)

The changes convert to unsigned integer constants because shifting is best done with unsigned integers.

The expression in statement 2, is still failing because of the right hand operator (9U) needs checking. According to MISRA, if the right hand operator is larger than the bit width of the underlying type of the left hand operator, there is a problem.

The base of the problem is that 1U has an underlying type of unsigned char or 8-bits.
The register we are writing to is 16-bits, so theoretically there is not an issue.

How can I change the expression in [2] so that it passes MISRA C 2004, preferring not to use casts?

I'm using IAR Embedded Workbench with an ARM7TDMI processor in 8/32 bit mode.

Edit 1: Example code.

void turn_on_motor(void);
#define MOTOR_ON (1U << 9U)
void turn_on_motor(void)
{
    uint16_t * const p_motor_control = (uint16_t *)(0x01234567U);
    *p_motor_control = MOTOR_ON;
}

Error text: Constant used as the right-hand operand of a shift operator shall be limited.

From the MISRA Rule documentation provided by Parasoft:

Rule reports a violation if:

- the right-hand operand is a constant with negative value or with value that
  exceeds the length (in bits) of the left-hand operand

- the right-hand operand is not a constant and is not checked by specific
  pattern

解决方案

You could also simply circumvent the shifting issue by using

 #define MOTOR_ON ((uint16_t)512U)   /* 1 << 9 = 2^9 */

这篇关于如何使(1 LT;&LT; 9)通过MISRA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆