为什么在MISRA:2012中需要功能原型? [英] Why function prototypes are they required in MISRA:2012?

查看:142
本文介绍了为什么在MISRA:2012中需要功能原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么MISRA:2012需要功能原型.在下面的示例中,这两个原型并不是必需的.

I am wondering why function prototypes are required by MISRA:2012. In the example below, the two prototypes aren't really necessary.

#include <stdio.h>
#include <stdlib.h>

// >>> Truly useless in my opinion
void display(void);
int main(void);
// <<<

void display(void) {
    printf("Hello World!\n");
}

int main() {
    display();
    return EXIT_SUCCESS;
}

我可以在SO上阅读的基本原理,例如

The rationale I can read on SO such as here isn't very clear to me. For instance, if main tries to access display before it is declared, the compiler or the static analyzer will raise an error: function display used before declaration.

换句话说,为该MISRA规则创建偏差是个好主意吗?

In other words is it a good idea to create a deviation for this MISRA rule?

推荐答案

void display(void);是函数正向声明.它具有原型格式.

void display(void); is a function forward declaration. It has prototype format.

如发布的链接所示,函数 prototype 是具有所有指定参数类型的函数声明.如果没有参数,则参数列表必须为(void)(无参数)而不是()(任何参数).

As indicated in the link posted, a function prototype is a function declaration with the types of all parameters specified. If there are no parameters, then the parameter list must be (void) (no parameters) and not () (any parameter).

确切的规则8.2说:

规则8.2函数类型应为带有指定参数的 prototype 形式

提供的基本原理(读起来很好)提到,这是为了避免旧的K& R和C90程序未指定所有参数.只要函数声明中的参数类型与函数定义中的参数类型不冲突,C99仍在某种程度上允许这样做.

The rationale provided (read it, it is pretty good) mentions that this is to avoid old K&R and C90 programs where not all parameters are specified. C99 still allows this at some extent, as long as the parameter types in the function declaration don't collide with the parameter types in the function definition.

从本质上讲,该规则旨在禁止此类功能:

Essentially, the rule seeks to ban these kind of functions:

void func1 (x)  // K&R style
int x;
{}

void func2(x)  // sloppy style
{}

所有参数(如果有)必须指定类型和名称.

All parameters (if any) must have types and names specified.

但是,我在MISRA-C中找不到任何内容,要求您为每个函数编写函数声明.这意味着无论有没有函数声明,您的示例代码都将符合此MISRA规则.

However, I find nothing in MISRA-C that requires you to write a function declaration for each function. This means that your example code would conform to this MISRA rule with or without the function declaration.

尽管正如我在前面的答案中提到的那样,编写不带函数声明(原型格式)的.c文件是一种草率的做法.如果需要按特定顺序调用函数,则应通过程序设计,函数命名和注释/文档来使其清晰可见.不是按照恰好在.c文件中声明它们的顺序.

Though as I mentioned in a previous answer, writing .c files without function declarations (in prototype format) is sloppy practice. If your functions need to be called in a certain order, it should be made obvious by the program design, function naming and comments/documentation. Not by the order that they happen to be declared inside the .c file.

.c文件中声明一个函数的源代码行与该函数的行为/使用之间不应存在紧密的联系.

There should be no tight coupling between the source code line where a function is declared in the .c file and that function's behavior/use.

相反,应该按照逻辑上合理的顺序定义函数.编写.c文件的一种常用方法是保留所有公共函数,这些函数的函数声明保存在.c文件顶部的.h文件中.然后,将内部功能(具有static/内部链接的功能)置于底部.该模型需要所有内部函数的函数声明.另一种选择是将所有内部功能放在顶部,将公共功能放在底部.只要您保持一致,就可以.

Instead, functions should be defined in an order that makes sense logically. A common way to write .c files is to keep all public functions, that have their function declaration up in the .h file, at the top of the .c file. Then let the internal functions (those with static/internal linkage) sit at the bottom. This model requires function declarations of all the internal functions. Another option is to put all internal functions on top, and the public functions at the bottom. As long as you are consistent, either is fine.

最重要的是,如果 .c文件中的函数定义重新排序,它不应破坏程序或引起编译器错误.最简单的方法是始终为程序中的每个函数提供函数声明.

What's most important is that if function definitions inside the .c file are re-ordered, it should not break the program or cause compiler errors. The easiest way to ensure this is to always provide function declarations for every single function in your program.

请注意,文件顶部的函数声明根本不是真正没用的",因为它们提供了C文件中存在的所有函数的快速摘要.这是一种编写自文档代码的方法.

Note that the function declarations on top of the file is not "truly useless" at all, as they provide a quick summary of all functions present in the C file. It is a way to write self-documenting code.

请注意,作为一种特殊情况,C标准不允许main()的原型.

Note that the C standard allows no prototype for main(), as a special case.

请注意,此外,规则8.7和8.8禁止在不使用static的情况下使用void display(void),因为该功能仅在一个翻译单元中使用.

Note that in addition, rule 8.7 and 8.8 disallows you to use void display(void) without static, since the function is only used in one translation unit.

这篇关于为什么在MISRA:2012中需要功能原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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