C编译器断言 - 如何实现? [英] C compiler asserts - how to implement?

查看:125
本文介绍了C编译器断言 - 如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个断言是prevents编译,而不是在运行时发生故障,错误情况。

I'd like to implement an "assert" that prevents compilation, rather than failing at runtime, in the error case.

我现在有一个这样的,伟大的工程定义,但是这增加了二进制文件的大小。

I currently have one defined like this, which works great, but which increases the size of the binaries.

#define MY_COMPILER_ASSERT(EXPRESSION) switch (0) {case 0: case (EXPRESSION):;}

样code(其中不能编译)。

Sample code (which fails to compile).

#define DEFINE_A 1
#define DEFINE_B 1
MY_COMPILER_ASSERT(DEFINE_A == DEFINE_B);

我如何能实现这一点,以便它不产生任何code(以尽量减少所产生的二进制文件的大小)?

How can I implement this so that it does not generate any code (in order to minimize the size of the binaries generated)?

感谢您的帮助。
NickB

Thanks for any help. NickB

推荐答案

在纯标准C编译时断言是可能的,而且preprocessor挂羊头卖狗肉的一点点使得它的使用看起来就像干净运行时间使用的断言()

A compile-time assert in pure standard C is possible, and a little bit of preprocessor trickery makes its usage look just as clean as the runtime usage of assert().

关键关键是要找到可以在编译时进行评估,并可能导致错误的一些值的结构。答案之一是一个数组的声明不能有负面的大小。使用的typedef prevents的成功空间的分配和preserves失败的错误。

The key trick is to find a construct that can be evaluated at compile time and can cause an error for some values. One answer is the declaration of an array cannot have a negative size. Using a typedef prevents the allocation of space on success, and preserves the error on failure.

错误消息本身会含糊地指大小为负的声明(GCC说:数组富的大小为负),所以你应该挑选的提示,这个错误确实是一个断言检查数组类型的名字。

The error message itself will cryptically refer to declaration of a negative size (GCC says "size of array foo is negative"), so you should pick a name for the array type that hints that this error really is an assertion check.

一个进一步的问题来处理的是,它是唯一可能为的typedef 特定类型的名称,便可以在任何编译单元。因此,宏已为每个使用来获得一个独特的类型名称来声明安排。

A further issue to handle is that it is only possible to typedef a particular type name once in any compilation unit. So, the macro has to arrange for each usage to get a unique type name to declare.

我的通常的解决方案一直到需要该宏具有两个参数。首先是断言条件为真,第二个是幕后声明的类型名称的一部分。在使用标记粘贴和 __ __ LINE predefined宏可能形成独特的名称,而不需要一个额外的参数。

My usual solution has been to require that the macro have two parameters. The first is the condition to assert is true, and the second is part of the type name declared behind the scenes. The answer by plinth hints at using token pasting and the __LINE__ predefined macro to form a unique name possibly without needing an extra argument.

不幸的是,如果断言检查是在一个包含的文件,它仍然可以用在第二同一行号码的检查碰撞包含的文件,或在主源文件中的该行号。我们可以纸翻面,通过使用宏 __ FILE __ ,但它被定义为一个字符串常量并没有preprocessor窍门,可以把一个字符串常量回标识符名称的一部分;更不用说法律文件名称可以包含不标识符的法律部分字符。

Unfortunately, if the assertion check is in an included file, it can still collide with a check at the same line number in a second included file, or at that line number in the main source file. We could paper over that by using the macro __FILE__, but it is defined to be a string constant and there is no preprocessor trick that can turn a string constant back into part of an identifier name; not to mention that legal file names can contain characters that are not legal parts of an identifier.

所以,我提出以下建议code片段:

So, I would propose the following code fragment:

/** A compile time assertion check.
 *
 *  Validate at compile time that the predicate is true without
 *  generating code. This can be used at any point in a source file
 *  where typedef is legal.
 *
 *  On success, compilation proceeds normally.
 *
 *  On failure, attempts to typedef an array type of negative size. The
 *  offending line will look like
 *      typedef assertion_failed_file_h_42[-1]
 *  where file is the content of the second parameter which should
 *  typically be related in some obvious way to the containing file
 *  name, 42 is the line number in the file on which the assertion
 *  appears, and -1 is the result of a calculation based on the
 *  predicate failing.
 *
 *  \param predicate The predicate to test. It must evaluate to
 *  something that can be coerced to a normal C boolean.
 *
 *  \param file A sequence of legal identifier characters that should
 *  uniquely identify the source file in which this condition appears.
 */
#define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)

#define _impl_PASTE(a,b) a##b
#define _impl_CASSERT_LINE(predicate, line, file) \
    typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];

一个典型的用法可能是这样的:

A typical usage might be something like:

#include "CAssert.h"
...
struct foo { 
    ...  /* 76 bytes of members */
};
CASSERT(sizeof(struct foo) == 76, demo_c);

在GCC,断言失败将类似于:

In GCC, an assertion failure would look like:


$ gcc -c demo.c
demo.c:32: error: size of array `assertion_failed_demo_c_32' is negative
$

这篇关于C编译器断言 - 如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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