Program analysis and editing of C ++ source files

I want to programmatically analyze and edit C ++ source files. I need to change / add code to certain sections of code (i.e. In functions, class blocks, etc.). I also (preferably) could also get comments.

Part of what I want to do can be explained by the following code snippet:

CPlusPlusSourceParser cp = new CPlusPlusSourceParser("x.cpp");  // Create C++ Source Parser Object
CPlusPlusSourceFunction[] funcs = cp.getFunctions();  // Get all the functions

for (int i = 0; i &lt funcs.length; i++) {  // Loop through all functions
    funcs[i].append(/* … code I want to append …*/);  // Append some code to function 
}
cp.save(); // Save new source
cp.close(); // Close file

How can i do this?

I'd like to do this preferably in Java, C ++, Perl, Python or C #. However, I am open to other languages ​​API.

+5
source share
7 answers

This is similar to AST from C code

Java antlr , . AST- - .

+4

++ , CFG (- ) ++, Antlr

Edit:

Antlr

+2

++, , , , . , .

+2

A ++ DMS Software Reengineering . ++, ANSI, GNU 3/4, MSVS6 MSVisual Studio 2005 ++.

AST ( , ). , C++ , .

+2

doxygen, , , ++. , lexer

+1

Mozilla , .


Clang , ++. Stroustrup Texas A & M, , .

+1

# general.net ++/CLI CodeDOM - ++- , , .

+1

All Articles