MISRA C 2012是否说不使用布尔 [英] Does MISRA C 2012 say not to use bool

查看:249
本文介绍了MISRA C 2012是否说不使用布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于为新项目制定框架的早期阶段.

I am in the early stages of framing stuff out on a new project.

我定义了一个返回类型为"bool"的函数

I defined a function with a return type of "bool"

我从PC-Lint获得了此输出

I got this output from PC-Lint

    Including file sockets.h (hdr)
bool sock_close(uint8_t socket_id);
^
"LINT: sockets.h (52, 1) Note 970: Use of modifier or type '_Bool' outside of a typedef [MISRA 2012 Directive 4.6, advisory]"

我继续在另一个头文件中定义了它,以关闭皮棉:

I went ahead and defined this in another header to shut lint up:

typedef bool bool_t;

然后我开始怀疑为什么我必须这样做以及为什么它改变了一切.我转向MISRA 2012 Dir 4.6.它主要关注的是诸如short,int和long之类的原始类型的宽度,其宽度以及如何对其进行签名.

Then I started wondering why I had to do that and why it changed anything. I turned to MISRA 2012 Dir 4.6. It is concerned mostly about the width of primitive types like short, int, and long, their width, and how they are signed.

该标准没有给出布尔的任何放大,有理,例外或示例.

The standard does not give any amplification, rational, exception, or example for bool.

bool在C99中的stdbool.h中明确定义为_Bool.那么这个标准真的适用于布尔吗?

bool is explicitly defined as _Bool in stdbool.h in C99. So does this criteria really apply bool?

根据C99的6.2.5节,我认为_Bool始终是足以存储值0和1的最小标准无符号整数类型".因此,我们知道bool是未签名的.难道不是_Bool宽度不是固定的,而是以某种方式提升主题的问题吗?因为理性似乎与该观念相矛盾.

I thought _Bool was explicitly always the "smallest standard unsigned integer type large enough to store the values 0 and 1" according to section 6.2.5 of C99. So we know bool is unsigned. Is it then just a matter of the fact that _Bool is not fixed width and subject being promoted somehow that's the issue? Because the rational would seem to contradict that notion.

遵守此准则并不能保证可移植性,因为int类型的大小可能确定表达式是否要进行整数提升.

Adherence to this guideline does not guarantee portability because the size of the int type may determine whether or not an expression is subject to integer promotion.

仅放置typedef bool bool_t;会发生什么变化-因为我在这样做时没有表示宽度或符号? bool_t的宽度也将取决于平台.有没有更好的方法来重新定义布尔值?

How does just putting typedef bool bool_t; change anything - because I do nothing to indicate the width or the signdedness in doing so? The width of bool_t will just be platform dependent too. Is there a better way to redefine bool?

除非已实现的类型实际上具有该长度,否则不得定义特定长度的类型

A type must not be defined with a specific length unless the implemented type is actually of that length

所以typedef bool bool8_t;应该完全是非法的.

so typedef bool bool8_t; should be totally illegal.

金普尔(Gimpel)对指令4.6的解释是错误的还是被发现?

Is Gimpel wrong in their interpretation of Directive 4.6 or are they spot on?

推荐答案

在typedef之外使用修饰符或键入'_Bool'[MISRA 2012 Directive 4.6,咨询]

Use of modifier or type '_Bool' outside of a typedef [MISRA 2012 Directive 4.6, advisory]

那是胡说八道,指令4.6只关心在stdint.h中使用类型,而不是在intshort等中使用.该指令与基本数字类型有关. bool与该指令无关,因为它不是数字类型.

That's nonsense, directive 4.6 is only concerned about using the types in stdint.h rather than int, short etc. The directive is about the basic numerical types. bool has nothing to do with that directive whatsoever, as it is not a numerical type.

出于未知的原因,MISRA-C:2012示例使用名为bool_t的怪异类型,这是非标准的.但是MISRA绝不强制将这种类型用于任何地方,特别是他们没有在指令4.6中强制实施它,甚至没有提到布尔值. MISRA不鼓励在任何地方使用bool_Bool.

For reasons unknown, MISRA-C:2012 examples use a weird type called bool_t, which isn't standard. But MISRA does by no means enforce this type to be used anywhere, particularly they do not enforce it in directive 4.6, which doesn't even mention booleans. MISRA does not discourage the use of bool or _Bool anywhere.

金普尔(Gimpel)对指令4.6的解释是错误的

Is Gimpel wrong in their interpretation of Directive 4.6

是的,他们的工具给出了错误的诊断信息.

Yes, their tool is giving incorrect diagnostics.

此外,您可能必须配置该工具(如果可能)以告诉它所使用的布尔类型. 5.3.2提到如果不使用_Bool,则可能必须这样做,这意味着所有静态分析器都必须理解_Bool.但是,即使正确配置了bool类型,dir 4.6也与此无关.

In addition, you may have to configure the tool (if possible) to tell it which bool type that is used. 5.3.2 mentions that you might have to do so if not using _Bool, implying that all static analysers must understand _Bool. But even if the bool type is correctly configured, dir 4.6 has nothing to do with it.

这篇关于MISRA C 2012是否说不使用布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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