add a macro call at start of every function definition in c++ source file

619 views Asked by At

I would like to add a single line of code at the beginning of each function in my c++ visual studio 2015 project.

It would take months to manually add a line into each function. Are there any quick way or tool to solve this problem ?

function examples are:

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

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

Class1::~Class1() { //some code }

I need to handle all of these function definitions

Sample:

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
}

I found similar answers explaining about regex,aspect programming,__pender. As I am a beginner, its hard to understand those concepts.

Expectation is: I would like to give the path of workspace folder and tool will fetch all cpp files in that and add the macro at required place.

If this tool doesn't exist,Please guide if similar tool can be build using any technology like .net,java or python.

1

There are 1 answers

6
kesarling He-Him On

Replace (^.*\(.*\).*\n{)\n(.*) with $1\nmyMacro\n$2


Explanation:

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

RE: I found similar answers explaining about regex,aspect programming,__pender. As I am a beginner, its hard to understand those concepts.
Use Find replace in Visual Studio ;)

The site linked in the demo also provides a code in C#/python which can be modified a little and can be used as a 'tool' to convert the files you need