Re: detect empty functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 05 Feb 2014, Prathamesh Kulkarni wrote:

> Hi, is there a way to find if a function has empty body ?
>
There might be some way with gcc - but you might also want to consider
tools that are maybe more suitable for the task like coccinelle
Here is a simple example of detecting empty functions with coccinelle

the source file:
----------------

void foo(void){
}

int bar(int x){
}

int baz(int x){
        return x+1;
}

int main(int argc, char ** argv){
        int ret=-1;
        foo();
        ret=bar(0);
        ret=baz(ret);
        return ret;
}

the cocci script for detection and reporting:
---------------------------------------------

@empty@
identifier func;
position p1;
@@

func@p1(...){
}

@script:python@
p1 << empty.p1;
fn << empty.func;
@@

print "%s:%s empty function at lines %s" % (p1[0].file,fn,p1[0].line)

running it:
-----------

and the result of executing spatch on this hello.c

hofrat@debian:/tmp$ spatch -sp_file empty.cocci hello.c
init_defs_builtins: /usr/local/share/coccinelle/standard.h
HANDLING: hello.c
hello.c:bar empty function at lines 4
hello.c:foo empty function at lines 1

Not saying that gcc cant do it somehow - but it just might not be the
right tool to use - there are open-source tools around for checking 
source-code properties that seem more suitable for your problem.

HTH

thx!
hofrat 




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux