简体   繁体   English

严格的ISO C一致性测试

[英]Strict ISO C Conformance Test

I am currently working on a C project that needs to be fairly portable among different building environments. 我目前正在研究一个C项目,该项目需要在不同的建筑环境之间具有相当的可移植性。 The project targets POSIX-compliant systems on a hosted C environment. 该项目针对托管C环境中符合POSIX的系统。

One way to achieve a good degree of portability is to code under conformance to a chosen standard, but it is difficult to determine whether a given translation unit is strict-conformant to ISO C. For example, it might violate some translation limits, or it might be relying on an undefined behavior, without any diagnostic message from the compilation environment. 实现良好可移植性的一种方法是按照所选标准进行编码,但是很难确定给定的翻译单元是否严格符合ISOC。例如,它可能违反某些翻译限制,或者可能依赖于未定义的行为,而编译环境中没有任何诊断消息。 I am not even sure whether it is possible to check for strict conformance of large projects. 我什至不确定是否有可能检查大型项目的严格符合性。

With that in mind, is there any compiler, tool or method to test for strict ISO C conformance under a given standard (for example, C89 or C99) of a translation unit? 考虑到这一点,是否有任何编译器,工具或方法可以在给定的翻译单元标准(例如C89或C99)下测试严格的 ISO C一致性?

Any help is appreciated. 任何帮助表示赞赏。

It is not possible in general to find undefined run-time behavior. 通常,不可能找到未定义的运行时行为。 For example, consider 例如,考虑

void foo(int *p, int *q)
{
    *p = (*q)++;
    ...

which is undefined if p == q . 如果p == q ,则未定义。 Whether that can happen can't be determined ahead of time without solving the halting problem. 如果不解决停止问题,就无法提前确定是否会发生。

(Edited to fix mistake caf pointed out. Thanks, caf.) (编辑以纠正caf指出的错误。谢谢,caf。)

Not really. 并不是的。 The C standard doesn't set any absolute minimum limits on translation units that must be accepted. C标准未对必须接受的翻译单位设置任何绝对最低限制。 As such, a perfectly accurate checker would be trivial to write, but utterly useless in practice: 这样,编写一个非常准确的检查器将是微不足道的,但是在实践中完全没有用:

#include <stdio.h>

int main(int argc, char **argv) { 
    int i;
    for (i=1; i<argc; i++)
        fprintf(stderr, "`%s`: Translation limit (potentially) exceeded.\n", argv[i]);
    return 0;
}

Yes, this rejects everything , no matter how trivial. 是的,无论多么琐碎,这都会拒绝一切 That is in accordance with the standard. 这符合标准。 As I said, it's utterly useless in practice. 正如我所说,在实践中它完全没有用。 Unfortunately, you can't really do a whole lot better -- when you decide to port to a different implementation, you could run into some oddball resource limit you've never seen before, so any code you write (up to an including "hello world") could potentially exceed a resource limit despite being allowed by dozens or even hundreds of compilers on/for much smaller systems. 不幸的是,您并不能真正做得更好-当您决定移植到其他实现时,可能会遇到一些以前从未见过的怪异的资源限制,因此,您编写的任何代码(包括“ hello world”),尽管在小型系统上使用了数十甚至数百个编译器,但仍可能超出资源限制。

Edit: 编辑:

Why a "hello world" program isn't strictly conforming 为什么“ hello world”程序不严格符合

First, it's worth re-stating the definition of "strictly conforming": "A strictly conforming program shall use only those features of the language and library specified in this International Standard.2) It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit." 首先,值得重申“严格符合”的定义:“严格符合程序应仅使用本国际标准中指定的语言和库的那些功能。2)不应产生依赖于任何未指定,未定义,或实施定义的行为,并且不得超过任何最低实施限制。”

There are actually a number of reasons "Hello, World" isn't strictly conforming. 实际上,有很多原因导致“ Hello,World”不严格符合。 First, as implied above, the minimum requirements for implementation limits are completely meaningless -- although there has to be some program that meets certain limits that will be accepted, no other program has to be accepted, even if it doesn't even come close to any of those limits. 首先,如上提示,实施限制的最低要求是完全没有意义的-虽然,必须有一定的程序,符合将要接受一定的限制, 没有其他程序必须接受,即使它甚至不能接近这些限制中的任何一个。 Given the way the requirement is stated, it's open to question (at best) whether there is any such thing as a program that doesn't exceed any minimum implementation limit, because the standard doesn't really define any minimum implementation limits. 给出要求的方式后,(该程序最多)还有一个疑问,即程序是否存在不超过任何最小实施限制的事情,因为该标准并未真正定义任何最小实施限制。

Second, during phase 1 of translation: "Physical source file multibyte characters are mapped, in an implementation defined manner, to the source character set ... " (§5.1.1.2/1). 其次,在翻译的第1阶段:“将物理源文件多字节字符以实​​现定义的方式映射到源字符集...”(第5.1.1.2/1节)。 Since "Hello, World!" 自“你好,世界!” (or whatever variant you prefer) is supplied as a string literal in the source file, it can be (is) mapped in an implementation-defined manner to the source character set. (或您喜欢的任何变体)在源文件中作为字符串文字提供,可以按实现定义的方式将其映射到源字符集。 An implementation is free to decide that (for an idiotic example) string literals will be ROT13 encoded, and as long as that fact is properly documented, it's perfectly legitimate. 一个实现可以自由决定(对于一个愚蠢的例子)字符串文字将是ROT13编码的,并且只要对该事实进行了适当的记录,它是完全合法的。

Third, the output is normally written via stdout . 第三,通常通过stdout写入stdout stdout is a text stream. stdout是文本流。 According to the standard: "Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment. Thus, there need not be a one-to-one correspondence between the characters in a stream and those in the external representation." 根据该标准:“可能必须在输入和输出上添加,更改或删除字符,以符合主机环境中表示文本的不同约定。因此,字符之间不必一一对应。在流和外部表示中。” (§7.19.2/2) As such, an implementation could (for example) do Huffman compression on the output (on Monday, Wednesday, or Friday). (第7.19.2 / 2节)因此,实现可以(例如)对输出(星期一,星期三或星期五)进行霍夫曼压缩。

So, we have (at least) three distinct points at which the output from a "Hello, World!" 因此,我们(至少)有三个不同的点,它们分别来自“ Hello,World!”的输出。 depends on implementation-defined characteristics -- any one of which would prevent it from fitting the definition of a strictly conforming program. 取决于实现定义的特征-任何一个特征都将阻止它符合严格符合程序的定义。

gcc has warning levels that will attempt to pin down various aspects of ANSI conformance. gcc具有警告级别,将尝试确定ANSI一致性的各个方面。 But hat's only a starting point. 但是帽子只是一个起点。

您可以从gcc -std=c99gcc -ansi -pedantic

Good luck with that. 祝你好运。 Try to avoid signed integers, because: 尝试避免带符号整数,因为:

int f(int x) 
{
 return -x;
}

can invoke UB. 可以调用UB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM