Linux内核中的typecheck宏如何工作? [英] How does the typecheck macro from the Linux kernel work?

查看:76
本文介绍了Linux内核中的typecheck宏如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux内核4.16的文件include/linux/typecheck.h包含此代码.

The file include/linux/typecheck.h of the Linux kernel 4.16 contains this code.

#define typecheck(type,x) \
({      type __dummy; \
        typeof(x) __dummy2; \
        (void)(&__dummy == &__dummy2); \
        1; \
}

检查x是否与参数type相同.

但是我听不懂这行:

 (void)(&__dummy == &__dummy2);

比较两个变量的首地址有何帮助?

How does comparing the first address of both variables help?

推荐答案

将指针与不兼容的类型进行比较会违反约束,并且要求编译器发出诊断信息.请参阅6.5.9相等运算符:

Comparing pointers with incompatible types is a constraint violation and requires the compiler to issue a diagnostic. See 6.5.9 Equality operators:

约束

应满足以下条件之一:

  • 两个操作数均具有算术类型;
  • 两个操作数都是指向兼容类型的合格或不合格版本的指针;
  • 一个操作数是一个指向对象或不完整类型的指针,另一个是一个指向void的合格或不合格版本的指针;或
  • 一个操作数是一个指针,另一个是空指针常量.
  • both operands have arithmetic type;
  • both operands are pointers to qualified or unqualified versions of compatible types;
  • one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void; or
  • one operand is a pointer and the other is a null pointer constant.

和5.1.1.3诊断:

and 5.1.1.3 Diagnostics:

即使预处理翻译单元或翻译单元包含违反任何语法规则或约束的条件,即使该行为也被明确指定为未定义,符合标准的实现也应至少产生一条诊断消息(以实现定义的方式标识)或实施定义.在其他情况下,无需生成诊断消息.

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.

这篇关于Linux内核中的typecheck宏如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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