How to extract one function from the source file

I am working on a small academic study of extremely long and complex functions in the Linux kernel . I am trying to find out if there are good reasons for writing 600 or 800 lines.

For this purpose, I would like to find a tool that can extract a function from a .c file, so I can run some automated tests for this function.

For example, if I have the cifs_parse_mount_options() function in the connect.c file, I am looking for a solution that will work roughly like:

 extract /fs/cifs/connect.c cifs_parse_mount_options 

and return 523 lines of code (!) of the function, starting from the opening curly braces to the closing curly braces.

Of course, any way to manipulate existing software packages such as gcc will also be useful.

Thanks,

Udi

EDIT: Regex answers to pull prototype declarations of C function? convinced me that declaring a function with regex is far from trivial.

+4
source share
3 answers

Why don't you write a small PERL / PHP / Python script, or even a small C ++, Java, or C # program that does this?

I do not know any tools already done for this, but for writing code to analyze a text file and extracting the body of a function from a C ++ code file, it should not take more than 20 lines of code. The only tricky part will determine the start of the function, and this should be a relatively simple task using RegEx. After that, all you need to do is iterate over the rest of the file, tracking the opening and closing of the curly braces, and when you reach the closing brace of the function, you are done.

+3
source

indent -kr code -o code.out

awk -f split.awk code.out

you need to adapt the split.awk bit a bit, which is somewhat specific to my code and refactoring needs (for example, they have such a structure that are not typedefs

And I'm sure you can make a nicer script :-)

 -- BEGIN { line=0; FS=""; out=ARGV[ARGC-1] ".out"; var=ARGV[ARGC-1] ".var"; ext=ARGV[ARGC-1] ".ext"; def=ARGV[ARGC-1] ".def"; inc=ARGV[ARGC-1] ".inc"; typ=ARGV[ARGC-1] ".typ"; system ( rm " " -f " " out " " var " " ext " " def " " inc " " typ ); } /^[ ]*\/\/.*/ { print "comment :" $0 "\n"; print $0 >> out ; next ;} /^#define.*/ { print "define :" $0 ; print $0 >>def ; next;} /^#include.*/ { print "define :" $0 ; print $0 >>inc ; next;} /^typedef.*{$/ { print "typedef var :" $0 "\n"; decl="typedef";print $0 >> typ;infile="typ";next;} /^extern.*$/ { print "extern :" $0 "\n"; print $0 >> ext;infile="ext";next;} /^[^ }].*{$/ { print "init var :" $0 "\n";decl="var";print $0 >> var; infile="vars"; print $0; fout=gensub("^([^ \\*])*[ ]*([a-zA-A0-9_]*)\\[.*","\\2","g") ".vars"; print "var decl : " $0 "in file " fout; print $0 >fout; next; } /^[^ }].*)$/ { print "func :" $0 "\n";decl="func"; infile="func"; print $0; fout=gensub("^.*[ \\*]([a-zA-A0-9_]*)[ ]*\\(.*","\\1","g") ".func"; print "function : " $0 "in file " fout; print $0 >fout; next; } /^}[ ]*$/ { print "end of " decl ":" $0 "\n"; if(infile=="typ") { print $0 >> typ; }else if (infile=="ext"){ print $0 >> ext; }else if (infile=="var") { print $0 >> var; }else if ((infile=="func")||(infile=="vars")) { print $0 >> fout; fflush (fout); close (fout); }else if (infile=="def") { print $0 >> def; }else if (infile=="inc"){ print $0 >> inc; }else print $0 >> out; next; } /^[a-zA-Z_]/ { print "extern :" $0 "\n"; print $0 >> var;infile="var";next;} { print "other :" $0 "\n" ; if(infile=="typ") { print $0 >> typ; }else if (infile=="ext"){ print $0 >> ext; }else if (infile=="var") { print $0 >> var; }else if ((infile=="func")||(infile=="vars")){ print $0 >> fout; }else if (infile=="def") { print $0 >> def; }else if (infile=="inc"){ print $0 >> inc; }else print $0 >> out; next; } 
+1
source

if you find it difficult to find function names:

1> use ctags (program) to extract function names. ctags -x --c-species = fp path_to_file. 2> After you get the function names, write a simple perl script to extract the contents of the function by passing the name of the script function as described above.

0
source

All Articles