c++ - 在 C++ 源文件中每个函数定义的开头添加一个宏调用

标签 c++ python-3.x visual-studio-2015 automation pypeg

我想在我的 c++ Visual Studio 2015 项目中的每个函数的开头添加一行代码。
在每个函数中手动添加一行需要几个月的时间。有没有快速的方法或工具来解决这个问题?
函数示例有:

void Class::Func1(CATUnicodeString& Record ) const{ //some code }

Class1::Class1():CATBaseUnknown() ,_usDrawingNumber( "" ){ //some code }

Class1::~Class1() { //some code }
我需要处理所有这些函数定义
样本:
void func1() 
{
    //code
}

int func2() 
{
    //code
}

char* func3() 
{
    //code
}

/* more functions */

bool func100()
{
    //code
}


//I want them to become:

void func1() 
{
    myMacro;
    //code
}

int func2() 
{
    myMacro;
    //code
}

char* func3() 
{
    myMacro;
    //code
}

/* more functions */

bool func100() 
{
    myMacro;
    //code
}
我找到了类似的答案来解释正则表达式、方面编程、__pender。由于我是初学者,很难理解这些概念。
期望是:
我想提供工作区文件夹的路径,工具将获取其中的所有 cpp 文件并在所需位置添加宏。
如果此工具不存在,请指导是否可以使用.net、java或python等任何技术构建类似的工具。

最佳答案

替换 (^.*\(.*\).*\n{)\n(.*)$1\nmyMacro\n$2
解释:

1st Capturing Group (^.*\(.*\).*\n{):
^ asserts position at start of a line
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\( matches the character ( literally (case sensitive)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\) matches the character ) literally (case sensitive)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\n matches a line-feed (newline) character (ASCII 10)
{ matches the character { literally (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)  

2nd Capturing Group (.*):
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed
Demo
回复:I found similar answers explaining about regex,aspect programming,__pender. As I am a beginner, its hard to understand those concepts.在 Visual Studio 中使用查找替换;)
演示中链接的站点还提供了 C#/python 中的代码可以稍作修改,可以用作转换所需文件的“工具”

关于c++ - 在 C++ 源文件中每个函数定义的开头添加一个宏调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63066501/

相关文章:

python - 创建 Pycharm 后更改项目解释器

python - 在 tkinter 的 Tk.after() 方法中使用 async/await 关键字

azure - 为什么我不能 "Configure Azure AD Authentication"

C++ 多个版本的默认特殊成员函数——MSVC 2015 中的错误

c++ - 从 Derived** 到 Base*const* 的转换

c++ - 什么是复制省略和返回值优化?

python - 确定文件相对于目录的路径,包括符号链接(symbolic link)

c++ - clCreateBuffer() 永远不会失败

具有私有(private)访问说明符的 C++ 派生类

c++ - 如何在 Visual Studio Community 2015 中创建 "ATL Project"?