宏插入到功能启动的自动化 [英] Automation of macro insertion to function start

查看:72
本文介绍了宏插入到功能启动的自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么自动方法可以将宏插入函数体的开头吗?

假设我有宏:

Hi all, is there any automated way to insert macro into start of function body?

Let''s say I have macro:

#define WRITE_LINE printf("%s - %d\n", __FUNCTION__, __LINE__);



和功能:



And a function:

void foo()
{
  DoSomething();
}



我想要以某种方式修改VisualStudio 2005项目中的所有功能,使其具有如下所示:



What I want is somehow modify all functions in VisualStudio 2005 project to have it like follows:

void foo()
{
  WRITE_LINE
  DoSomething();
}



有可能吗?

谢谢



Is it possible?

Thank you

推荐答案

您使用的是什么操作系统?您不能完全输入宏,但是可以使用在调用某个函数时就会调用的特殊函数.
在Windows中,此功能称为_penter.
假设您有一个名为void foo();
的函数
在呼叫站点是foo();
通过特殊的编译器设置,您可以将调用转到

Well what OS are you using? You cannot exactly enter a macro but you can use a special function that gets called when ever a function gets called.
In windows this function is called _penter.
Suppose you have a function called void foo();

At the call site it is foo();
With a special compiler setting you can turn the call to

_penter();
foo();
_pexit();



_penter和_pexit是VC ++编译器插入的特殊功能(如果启用该设置).如果需要,您可以在此_penter()中执行任何操作.但是请注意性能影响.

使用Visual Studio C/C ++编译器和DIA SDK的简单探查器 [



_penter and _pexit are special functions that the VC++ compiler inserts, if you enable the setting. If you want you can do anything in this _penter(). But be aware of the performance impact.

A Simple Profiler using the Visual Studio C/C++ Compiler and DIA SDK[^]

This article describes it.

There is a similar functionality for GCC also.


不要以为我曾经在Studio中找到实现此目的的方法,但是使用a脚本.您必须先解析并识别功能/方法定义,然后添加宏行.您可能会猜到,大部分工作都是在正确识别新函数/方法(但仍不算太难).
Don''t think I''ve ever seen a way to do this within Studio, but it would probably be easy enough to accomplish using a script. You would have to parse out and identify the function/method definitions first then add your macro line. Most of the work, as you might be able to guess, would be in correctly identifying a new function/method (but it still wouldn''t be too hard).


虽然您可以一定要按照解决方案1中所述检测代码,如果您的程序已经具有太多的功能,您不希望手动执行此操作,那么打印这样的每个函数的每个调用绝对不是一个好主意-看到会有多少个调用,我会感到很惊讶,而且由于printf是一个相当慢的功能,因此打印也会大大降低程序运行速度.

出于所有实际目的,您将始终必须手动启用或禁用(注释掉)这些命令,否则您将被太多文本淹没,以至于它根本对您没有任何用处.取消选中此复选框与尝试下载Internet相同;)
While you can surely instrument your code as described in Solution 1, if your program already has so many functions that you don''t want to do this manually, then it is definitely a bad idea to print out every call of every function like that - you''ll be surprised to see just how many calls that will be, and the printing might significantly slow your program as well, since printf is a rather slow function.

For all practical purposes you will always have to manually enable or disable (comment out) these commands, or you will be swamped with so much text that it is simply not of any use for you. Doing this unchecked is on the same lines as trying to download the internet ;)


这篇关于宏插入到功能启动的自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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