免费注册 查看新帖 |

Chinaunix

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

[文本处理] 用shell取得函数中定义的变量 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-13 00:03 |只看该作者 |倒序浏览
本帖最后由 jwz0620 于 2012-10-19 23:55 编辑

假设一个 .c 程序, 里面有函数定义若干, 函数中定义若干变量, 有什么办法可以把函数定义的所有变量按函数找出来?
如:
funtion_a(){

  int a, b;
  char bb[10];

  ........

  ........
}

funtion_b(){

  int a, b;

  ........

  ........
}

输出结果为:
函数名      变量名
funtion_a    a
funtion_a    b
funtion_a    bb
funtion_b    a
funtion_b    b

论坛徽章:
1
摩羯座
日期:2014-12-29 15:59:36
2 [报告]
发表于 2012-10-13 11:24 |只看该作者
用shell写个C parser吗?牛

论坛徽章:
1
辰龙
日期:2014-05-22 11:38:58
3 [报告]
发表于 2012-10-13 14:35 |只看该作者
本帖最后由 winway1988 于 2012-10-13 14:35 编辑
  1. $ cat test.c
  2. int
  3. f1(void)
  4. {
  5.     int a1, a2;
  6.     int b1;

  7.     printf("b1 = %d\n", b1);

  8.     return a1 + a2;
  9. }

  10. int
  11. f2(int a, int b)
  12. {
  13.     int a3 = a;
  14.     int b2 = b;

  15.     return a3 + b2;
  16. }

  17. int
  18. main()
  19. {
  20.     int a, b;

  21.     f1();
  22.     f2(a, b);

  23.     return 0;
  24. }
  25. $ cat awkscr
  26. /\(DW_TAG_subprogram\)/ {
  27.     flag = 1;
  28.     next;
  29. }

  30. flag == 1 && /DW_AT_name/ {
  31.     func_name = $NF;
  32.     flag = 2;
  33.     next;
  34. }

  35. flag == 2 && /\(DW_TAG_variable\)/ {
  36.     flag = 3;
  37.     next;
  38. }

  39. flag == 3 && /DW_AT_name/ {
  40.     print func_name, $NF;
  41.     flag = 2;
  42. }
  43. $ gcc -g -c test.c                        
  44. test.c: In function ‘f1’:
  45. test.c:7: warning: incompatible implicit declaration of built-in function ‘printf’
  46. $ readelf --debug-dump test.o | awk -f awkscr
  47. f1 a1
  48. f1 a2
  49. f1 b1
  50. f2 a3
  51. f2 b2
  52. main a
  53. main b
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2012-10-13 21:39 |只看该作者
楼上的方法不错,转换一下~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP