2

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.

usmanharoon
  • 173
  • 2
  • 13
  • 2
    I think the most proper way is to use the clang refractor tool (You need to parse your source files):http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1RefactoringTool.html – prehistoricpenguin Jul 24 '20 at 04:01
  • Depends on why you want to do it. If you want to use it as a debugging aid, it may be better to use a debugger to step through the code, rather than modifying it. – Peter Jul 24 '20 at 05:46
  • function examples are: 1. void Class::Func1(CATUnicodeString& Record ) const{ //some code } 2. Class1::Class1():CATBaseUnknown() ,_usDrawingNumber( "" ){ //some code } 3. Class1::~Class1() { //some code } I need to handle all of these function definitions. – usmanharoon Jul 27 '20 at 04:41

1 Answers1

0

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

kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
  • 1
    You're assuming that every function has consistent formatting, and that the parameter lists for functions are free from parentheses. Including an explanation of what the regex does would be helpful for those that do not speak regular expressions. – 1201ProgramAlarm Jul 24 '20 at 04:07
  • 2
    The demo is an off-site resource, and can be used as additional material but cannot be relied on. If it will "take months to manually add a line into each function" then that sounds like there is an awful lot of code, which would probably have been written by many different developers. There is no guarantee that _every single function_ would be formatted identically. In particular, lambdas and many one line functions declared in classes are often not formatted like global functions are. – 1201ProgramAlarm Jul 24 '20 at 04:13
  • Parameter list has parentheses and it has been written by multiple persons. so formatting is inconsistent. – usmanharoon Jul 25 '20 at 06:15
  • this regex didn't work. It didn't capture a single function definition. Please suggestion some better solution. – usmanharoon Jul 27 '20 at 04:55
  • @usmanharoon, I am sorry if it did not work :( I will try to find a better solution. You're free to Downvote it if you want :). Also are you sure you'd turned the regex switch in find replace on? Because it worked for me in my visual studio (2019) – kesarling He-Him Jul 27 '20 at 05:48
  • Regex switch was on. If you can help me to build a tool like i mentioned in question. that will be really helpful. – usmanharoon Jul 27 '20 at 08:55