免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 13095 | 回复: 61
打印 上一主题 下一主题

C语言带常量参数宏的编译时参数检查,有办法实现吗?(答案已经公开) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-01 11:42 |只看该作者 |倒序浏览
本帖最后由 没本 于 2012-02-01 18:45 编辑

定义一个带参数的宏

  1. #define MACRO_P(A) \
  2. { \
  3.     some.a = A; \
  4.     some.b = A+1; \
  5.     some.c[A] = 0; \
  6. }
  7. /* 用法,参数约定为只能是整型常量,范围在0..50 */
  8. MACRO_P(0);
  9. MACRO_P(33);
复制代码
其中参数A使用时必为常量,且在0..50范围内。那么如何在编译时判断越界并报错停止编译,而不是在运行时?
有可能实现吗?如果换成C++呢?

公开答案,其实就是用static_assert方法,这个方法是D语言最早官方支持,当然C和C++也都实现了,能保证程序的正确性并且没有运行时开销:
1. C_1x标准提供了_Static_assert()。gcc 4.6以后开始支持。
文件s.c

  1. /* C_1x standard _Static_assert version */
  2. #include <stdio.h>
  3. #define R 50
  4. #define M(A)                \
  5. {                        \
  6. _Static_assert( A>=0 && A<=R, "in M(A) A out of range" ); \
  7.         s.a = A;        \
  8.         s.b = A+1;        \
  9.         s.c[A] = 0;        \
  10. }
  11. typedef struct {
  12.         int a;
  13.         int b;
  14.         int c[R+1];
  15. } s_t;
  16. s_t s;
  17. int main(int argc, char ** argv)
  18. {
  19.         int i=0;
  20.         M(51);
  21.         M(i);
  22.         return 0;
  23. }

  24. /* compile time error message:
  25. $ gcc -o s s.c
  26. s.c: In function ‘main’:
  27. s.c:20:2: error: static assertion failed: "in M(A) A out of range"
  28. s.c:21:2: error: expression in static assertion is not constant
  29. $
  30. */
复制代码
2.一个不完美的数组下标不能为负的方案,ANSI C实现。
文件:a.c

  1. /* ANSI C version */
  2. #include <stdio.h>
  3. #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
  4. #define R 50
  5. #define M(A)                \
  6. {                        \
  7. STATIC_ASSERT( A>=0 && A<=R, in_M_A_out_of_range ); \
  8.         s.a = A;        \
  9.         s.b = A+1;        \
  10.         s.c[A] = 0;        \
  11. }
  12. typedef struct {
  13.         int a;
  14.         int b;
  15.         int c[R+1];
  16. } s_t;
  17. s_t s;
  18. int main(int argc, char ** argv)
  19. {
  20.         int i=0;
  21.         M(51);
  22.         M(i);
  23.         return 0;
  24. }

  25. /* compile time error message:
  26. $ gcc -o a a.c
  27. a.c: In function ‘main’:
  28. a.c:21:1: error: size of array ‘static_assertion_in_M_A_out_of_range’ is negative
  29. $
  30. 不完美,变量i作参数没有报错,原因是C99支持

  31. Z:\>cl a.c
  32. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
  33. Copyright (C) Microsoft Corporation.  All rights reserved.

  34. a.c
  35. a.c(21) : error C2118: negative subscript
  36. a.c(22) : error C2057: expected constant expression
  37. a.c(22) : error C2466: cannot allocate an array of constant size 0

  38. Z:\>
  39. msvc由于不支持C99,反而能找到第二个问题。
  40. */
复制代码
3. 利用gcc扩展的实现
文件:n.c

  1. /* GCC version */
  2. #include <stdio.h>
  3. #define CTC(X) ({ extern int __attribute__((error("assertion failure: '" #X "' not true"))) compile_time_check(); ((X)?0:compile_time_check()),0; })
  4. #define R 50
  5. #define M(A)                \
  6. {                        \
  7. CTC( A>=0 && A<=R ); \
  8.         s.a = A;        \
  9.         s.b = A+1;        \
  10.         s.c[A] = 0;        \
  11. }
  12. typedef struct {
  13.         int a;
  14.         int b;
  15.         int c[R+1];
  16. } s_t;
  17. s_t s;
  18. int main(int argc, char ** argv)
  19. {
  20.         int i=0;
  21.         M(51);
  22.         M(i);
  23.         return 0;
  24. }

  25. /* compile time error message:
  26. $ gcc -o n n.c
  27. n.c: In function ‘main’:
  28. n.c:21:2: error: call to ‘compile_time_check’ declared with attribute error: assertion failure: 'i>=0 && i<=R' not true
  29. n.c:22:2: error: call to ‘compile_time_check’ declared with attribute error: assertion failure: 'i>=0 && i<=R' not true
  30. $
  31. */
复制代码
4.利用位域的C实现,但用了__COUNTER__

  1. /* ANSI C version 2 */
  2. #include <stdio.h>
  3. #define CTASTR2(pre,post) pre ## post
  4. #define CTASTR(pre,post) CTASTR2(pre,post)
  5. #define STATIC_ASSERT(cond,msg) \
  6.     typedef struct { int CTASTR(static_assertion_failed_,msg) : !!(cond); } \
  7.         CTASTR(static_assertion_failed_,__COUNTER__)
  8. #define R 50
  9. #define M(A)                \
  10. {                        \
  11. STATIC_ASSERT( A>=0 && A<=R, in_M_A_out_of_range ); \
  12.         s.a = A;        \
  13.         s.b = A+1;        \
  14.         s.c[A] = 0;        \
  15. }
  16. typedef struct {
  17.         int a;
  18.         int b;
  19.         int c[R+1];
  20. } s_t;
  21. s_t s;
  22. int main(int argc, char ** argv)
  23. {
  24.         int i=0;
  25.         M(51);
  26.         M(i);
  27.         return 0;
  28. }

  29. /* compile time error message:
  30. $ gcc -o b b.c
  31. b.c: In function ‘main’:
  32. b.c:25:2: error: zero width for bit-field ‘static_assertion_failed_in_M_A_out_of_range’
  33. b.c:26:2: error: bit-field ‘static_assertion_failed_in_M_A_out_of_range’ width not an integer constant
  34. $

  35. Z:\>cl b.c
  36. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
  37. Copyright (C) Microsoft Corporation.  All rights reserved.

  38. b.c
  39. b.c(25) : error C2149: 'static_assertion_failed_in_M_A_out_of_range' : named bit
  40. field cannot have zero width
  41. b.c(26) : error C2057: expected constant expression
  42. b.c(26) : error C2149: 'static_assertion_failed_in_M_A_out_of_range' : named bit
  43. field cannot have zero width

  44. Z:\>
  45. */
复制代码

论坛徽章:
2
技术图书徽章
日期:2013-09-04 15:21:51酉鸡
日期:2013-11-01 21:20:20
2 [报告]
发表于 2012-02-01 12:35 |只看该作者
如果A是常量,那么MACRO_P(33);之类的就应该改成MACRO_P(SOME_VALUE);这样看代码才有意义,常量的范围也很容易控制,如果是库调用,那么被调用者应该动态防范溢出,而不能寄托于编译判断。

论坛徽章:
0
3 [报告]
发表于 2012-02-01 12:38 |只看该作者
回复 2# mirnshi


    我只是举例子,用数字比用常量名更能说明问题,这些地方就别较真了吧。如果6小时没有人能回答我就公布答案。

论坛徽章:
0
4 [报告]
发表于 2012-02-01 12:42 |只看该作者
这样也要报错吗?
const int a = 3333;
MACRO_P(a);

论坛徽章:
0
5 [报告]
发表于 2012-02-01 12:47 |只看该作者
AD8018 发表于 2012-02-01 12:42
这样也要报错吗?
const int a = 3333;
MACRO_P(a);

不允许这么填参数,只能是数字常量,不能是常变量,如果这个问题也能在编译时查出来当然更好。

论坛徽章:
0
6 [报告]
发表于 2012-02-01 13:05 |只看该作者
没本 发表于 2012-02-01 12:47
不允许这么填参数,只能是数字常量,不能是常变量,如果这个问题也能在编译时查出来当然更好。


那不简单,需要几个数组,就填几个数值.

下面的A允许的范围为 0~2


  1. #define CONCAT(X,Y)        X##Y

  2. #define Out_of_range_0
  3. #define Out_of_range_1
  4. #define Out_of_range_2


  5. #define MACRO_P(A)                                \
  6. {                                                                \
  7.         CONCAT(Out_of_range_,A)                \
  8.         printf("%d\n", A);                        \
  9. }

  10. int main()
  11. {
  12.         MACRO_P(1)       
  13.         MACRO_P(2)
  14.         MACRO_P(0)
  15.         //MACRO_P(10)
  16. }
复制代码

论坛徽章:
0
7 [报告]
发表于 2012-02-01 16:02 |只看该作者
回复 6# AD8018


    这个方法能用,要是数值范围比较大就会不大灵活。应该还有更好的方法。

论坛徽章:
0
8 [报告]
发表于 2012-02-01 16:11 |只看该作者
没本 发表于 2012-02-01 16:02
回复 6# AD8018


我坚信没有,要有也是写多个宏的变种。

要不公布答案吧,如果我输了,愿意免费赠送百元之内图书一本,请网购自选,发链接,快递到府上。。。。

论坛徽章:
0
9 [报告]
发表于 2012-02-01 16:12 |只看该作者
禁书除外

论坛徽章:
0
10 [报告]
发表于 2012-02-01 16:19 |只看该作者
回复 8# AD8018


    图书就算了,我积分也可不少。到时间再公布答案,而且不只一个,我查到的方法就有四五种。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP