www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Off subject

reply Vincent Richomme <forumer smartmobili.com> writes:
Hi,

sorry to ask my question here but since people are motivated by new 
language, source code, ... I thought it was a good place to ask my 
question about source code analysis.
I would like to know if there are some parsers/scripts that could parse 
some C/C++ language and that could insert printf in each function.

Actually I am trying to understand a huge program and debugging step by 
step would take me ages.
What I want is something like :

void myfunc(int aFoo)
{

...

}

I want a parser that could add

void myfunc(int aFoo)
{
fprintf(stderr, "myfunc(int aFoo)");
...

}
Jan 06 2009
next sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
Vincent Richomme wrote:
 Hi,
 
 sorry to ask my question here but since people are motivated by new 
 language, source code, ... I thought it was a good place to ask my 
 question about source code analysis.
 I would like to know if there are some parsers/scripts that could parse 
 some C/C++ language and that could insert printf in each function.
 
 Actually I am trying to understand a huge program and debugging step by 
 step would take me ages.
 What I want is something like :
 
 void myfunc(int aFoo)
 {
 
 ...
 
 }
 
 I want a parser that could add
 
 void myfunc(int aFoo)
 {
 fprintf(stderr, "myfunc(int aFoo)");
 ...
 
 }
I'm sure you can do this with CDT (the Eclipse plugin for C/C++). The API is here: http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.isv/reference/api/overview-summary.html I haven't worked with that API but more or less you'll need to: 1. Get an ITranslationUnit, which represents a source file. 2. From it, invoke getAST(). 3. Implement an IASTVisitor to traverse all the nodes and print them, with the exception of function bodies, where you'll add an pfritf. You could even make it a plugin and then just open the file in Eclipse, select an action from the menu and have it done for a whole project or a bunch of source files.
Jan 06 2009
prev sibling parent reply Jussi Jumppanen <jussij zeusedit.com> writes:
Vincent Richomme Wrote:

 I would like to know if there are some parsers/scripts that could parse 
 some C/C++ language and that could insert printf in each function.
One option would be to use a regular expression within a search and replace. Just as a simple test I came up regexp that does seems do the trick: Search string: (^[_a-z0-9]+[ &*\t]+[_a-z0-9 &*\t]*[_a-z0-9]+[ \t]*[(]+.*[^;]+)(\n{\n) Replace String: \0fprintf(stderr, ("\1") Any regexp enabled text editor should be able to handle the search and replace. It worked just fine in the Zeus IDE ;) You could also use SED or AWK to do a batch mode regexp search and replace. NOTE: The regexp was only tested for the function declaration that you posted, so it will almost certainly need to be tweaked for other variations of function definitions.
Jan 06 2009
parent reply Vincent Richomme <forumer smartmobili.com> writes:
Jussi Jumppanen a écrit :
 Vincent Richomme Wrote:
 
 I would like to know if there are some parsers/scripts that could parse 
 some C/C++ language and that could insert printf in each function.
One option would be to use a regular expression within a search and replace. Just as a simple test I came up regexp that does seems do the trick: Search string: (^[_a-z0-9]+[ &*\t]+[_a-z0-9 &*\t]*[_a-z0-9]+[ \t]*[(]+.*[^;]+)(\n{\n) Replace String: \0fprintf(stderr, ("\1") Any regexp enabled text editor should be able to handle the search and replace. It worked just fine in the Zeus IDE ;) You could also use SED or AWK to do a batch mode regexp search and replace. NOTE: The regexp was only tested for the function declaration that you posted, so it will almost certainly need to be tweaked for other variations of function definitions.
Hul doesn't work because it matches some typedef struct like this : typedef struct c_parser GTY(()) { ... } c_parser;
Jan 07 2009
parent Jussi Jumppanen <jussij zeusedit.com> writes:
Vincent Richomme Wrote:

 Hul doesn't work because it matches some typedef struct like this :
 
 typedef struct c_parser GTY(())
 {
 ...
 } c_parser;
That's what I meant when I said the regexp will almost certainly need to be tweaked ;) For example this new 'tweaked' regexp search string: (^[_a-z0-9]+[ &*\t]+[_a-z0-9 &*\t]*[_a-z0-9]+[ \t]*\({0,1}[^\]+.(.*[^;]+)(\n{\n) will distinguish bettween these two case: void myfunc(int aFoo) { ... } typedef struct c_parser GTY(()) { ... } c_parser; Obviously for each new case some extra tweaking of the repexp might be required. But if you used something like SED you can make the searching a hell of a lot smarter. SED allows you to define rules where by you can take the result of one search, run a second test against it and then accept or reject the result based on the result of this second test. http://www.zeusedit.com/forum/viewtopic.php?t=1229 Using this approach you could define rules to filter out these exceptions.
Jan 07 2009