How to "alternate" C / C ++ with my string (only inside functions in appropriate places)?

For example, there is a source:

void func1() {
    func3();
    if(qqq) {
         func2();
    }
    func4(
    );
}

It should be converted to:

void func1() {
MYMACRO
    func3();
MYMACRO
    if(qqq) {
MYMACRO
         func2();
MYMACRO
    }
MYMACRO
    func4(
    );
MYMACRO
}

those. to insert "MYMACRO \ n" at the end of each line, where the operator can be, only inside functions.

How to do it easily? Should I use regular expressions? What tools should be used?

For example, can gcc output all line numbers from all statements starting (or ending) inside functions?

@related How do I tell gcc to manage code with calls to my own function every _line_ of code?

@related Profiler, which should use _real_ time to measure (including waiting for system calls), spend on this function, not _CPU_ one

+2
3

, ? , , , . , , .


- . , , perl, python ruby, , C.

, , . . -, , . /*, " " , */. , , . , , . , , " ", , , {, , }. , , , . , , .. , , , :

void some_function (int arg) {
#ifdef CHECK_LIMIT_ONLY
    if (arg == 0) {
#else
    if (arg < 10) {
#endif
        // some code here
        ...
    }
}

, - , , . -, , . , , - .

, , , , , . , script , - , . , , , . , , , , , . , . { ... }. { }, .

, , script - , , . , , , . , - , .

'' Update: '' , , , . , . , . , , , .

+2

, : -)

gcc ... file1.c file2.c ... do

gcc ... `sed -e's/;/;\nMYMACRO/' file1.c` file1extra.c \
        `sed -e's/;/;\nMYMACRO/' file2.c` file2extra.c \
    ...
0

#. IO. , 3 . , "//FunctionStart" "//FunctionEnd" . , // .

, , , , .

        private void InsertMacro(string filePath)
        {
            //Declrations:
            StreamReader sr = new StreamReader(filePath);
            StreamWriter sw = new StreamWriter(filePath + ".tmp");
            string line = "";
            bool validBlock = false;

            //Go through source file line by line:
            while ((line = sr.ReadLine()) != null)
            {
                if (line == "//FunctionStart")
                    validBlock = true;
                else if (line == "//FunctionEnd")
                    validBlock = false;


                sw.WriteLine(line);

                if (validBlock)
                   sw.WriteLine("MYMACRO");
            }

            //Replace legacy source with updated source:
            File.Delete(filePath);
            File.Move(filePath + ".tmp", filePath);

            //Clean up streams:
            sw.Close();
            sr.Close();
        }
0

All Articles