Unix Programming - Help in automatically commenting functions in c files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Help in automatically commenting functions in c files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Help in automatically commenting functions in c files
jeniffer

2006-03-21, 3:17 am

Given a file with this table containing function name ,line number whr
func is found in file (field 3),the c file name (4th column) ,and first
line of the function ,

F3 function 14 ./test.c void F3()
H function 4 ./test1.c void H()
NotCalledFunc_Subdir function 3 ./trydir/a.c void
NotCalledFunc_Subdir()

if i need to comment all of them the the corresponding files ,how to
pick up one file at a time in a loop,open it and goto that line number?
Also for commenting the function in order to know its end by } bracket
is there a simple tool or wud i have to create a stack?

Pascal Bourguignon

2006-03-21, 3:17 am

"jeniffer" <zenith.of.perfection@gmail.com> writes:

> Given a file with this table containing function name ,line number whr
> func is found in file (field 3),the c file name (4th column) ,and first
> line of the function ,
>
> F3 function 14 ./test.c void F3()
> H function 4 ./test1.c void H()
> NotCalledFunc_Subdir function 3 ./trydir/a.c void
> NotCalledFunc_Subdir()
>
> if i need to comment all of them the the corresponding files ,how to
> pick up one file at a time in a loop,open it and goto that line number?
> Also for commenting the function in order to know its end by } bracket
> is there a simple tool or wud i have to create a stack?



The difficulty is in scanning the end bracket. Consider this C source:

------------------------------------------------------------------------
/* Compile with either:
gcc -ansi -trigraphs -c -o testc.o testc.c
gcc -c -o testc.o testc.c
*/
#include <setjmp.h>
#define ETMI 1
static int doit=1;

typedef struct BcTRY_EnvironmentT* BcTRY_EnvironmentP;
typedef struct BcTRY_EnvironmentT {
jmp_buf jmpEnv;
BcTRY_EnvironmentP next;
int code;
const void* data1;
const void* data2;
} BcTRY_EnvironmentT;

extern BcTRY_EnvironmentP BcTRY_CurrentEnvironment;


#define BcTRY \
{ BcTRY_EnvironmentT __tryEnvironment; \
__tryEnvironment.code=0; \
__tryEnvironment.next=BcTRY_CurrentEnvironment; \
BcTRY_CurrentEnvironment=&__tryEnvironment; \
if(setjmp(BcTRY_CurrentEnvironment->jmpEnv)==0){

#define BcHANDLER \
BcTRY_CurrentEnvironment=BcTRY_CurrentEn
vironment->next; \
}else{ \
BcTRY_CurrentEnvironment=BcTRY_CurrentEn
vironment->next;

#define BcEXCEPTIONCODE (__tryEnvironment.code)

#define BcENDTRY }}

#define trap(x)
void H3(){
BcTRY
will_it_raise_and_execption();
BcHANDLER
if(BcEXCEPTIONCODE==ETMI){
reduce_information();}
/* Ok, this is gross, one should use BcENDTRY: */ trap(}}}) }}}


void F(){
#ifdef __linux__
some_linux_stuff();}
#elif __windows__
some_windows_stuff();}
#endif

#if __STRICT_ANSI__
int a(int x){return x+1;} void H() ??<
if(doit) ??<
/* A little comment :-} */
printf("H :-} :-%c %s\n",'}',"\":-\\}\""); ??> ??>
#else
int a(int x){return x+1;} void H() {
if(doit) {
/* A little comment :-} */
// Another comment :-}
printf("H :-} :-%c %s\n",'}',"\":-\\}\""); } }
#endif

------------------------------------------------------------------------

You will need a parser, and you'll need to decide weither to manage
the pre-processor or not.

Counting the lines in a C source after the pre-processing is
different: you have to parse the output of the pre-processor.
You will have lines such as:
#line N file.c
and:
#file file.c
which you must take into account to reset the line numbers.



If you don't want to take the C pre-processor into account, and you
want to ignore the trigraphs, then parsing the C source for the end
bracket is rather easy:

Parser:

start ::= item start .
item ::= word | bracket .
word ::= text | string | character | c-comment | c++-comment .
bracket ::= "{" start "}" .


Scanner:

string ::= "\"([^"\\]|\\.)*\"".
character ::= "'([^'\\]|\\.)*'".
c-comment ::= "/\*([^*]|\*+[^/])*\*/" .
c++-comment ::= "//[^\n]*\n" .
text ::= "([^\"'{}/]|/[^*/])*" .


--
__Pascal Bourguignon__ http://www.informatimago.com/

"Our users will know fear and cower before our software! Ship it!
Ship it and let them flee like the dogs they are!"
Giorgos Keramidas

2006-03-21, 3:17 am

On Mon, 20 Mar 2006 14:21:22 +0100, Pascal Bourguignon <usenet@informatimago.com> wrote:
>"jeniffer" <zenith.of.perfection@gmail.com> writes:
>
> The difficulty is in scanning the end bracket. Consider this C source:
> [...]


One has to wonder though, isn't there an easy way to script Emacs to do
this? It already knows enough about moving around `C sexps' like
functions

/me runs before Pascal posts in less than half an hour the Elisp code

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com