All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Julia Lawall <julia.lawall@lip6.fr>
Cc: Shraddha Barke <shraddha.6596@gmail.com>,
	outreachy-kernel@googlegroups.com
Subject: Re: [Outreachy kernel] [PATCH 1/2] Staging: lustre: ptlrpc: Make functions static
Date: Thu, 01 Oct 2015 22:53:27 +0200	[thread overview]
Message-ID: <18050482.hzx1IbEyMT@wuerfel> (raw)
In-Reply-To: <alpine.DEB.2.10.1510011559340.2648@hadrien>

On Thursday 01 October 2015 16:12:53 Julia Lawall wrote:
> On Thu, 1 Oct 2015, Arnd Bergmann wrote:
> > On Thursday 01 October 2015 15:49:21 Julia Lawall wrote:
> > > On Thu, 1 Oct 2015, Arnd Bergmann wrote:
> > > > On Thursday 01 October 2015 15:20:39 Julia Lawall wrote:
> > > > > On Thu, 1 Oct 2015, Shraddha Barke wrote:
> > > > > > > Note that there are hundreds more symbols in lustre that should be made
> > > > > > > static.
> > > > > > > Most of them are harder to find because they have a declaration in a header
> > > > > > > file
> > > > > > > despite being only used in one place.
> > > > >
> > > > > Why does it make it hard to find?  One can make a spearate check in .c
> > > > > files.
> > > >
> > > > We currently get a sparse warning for each global identifier definition
> > > > that is not preceded by a declaration, but since most of our tools
> > > > (gcc, sparse, coccinelle, ...) work on a single file at a time, they
> > > > do not know whether a global symbol that is defined in a C file and
> > > > (incorrectly) declared in a header is used in another C file or not.
> > >
> > > OK, but from Coccinlle you can just call grep, and check the extensions on
> > > the returned file names, so it is not a big problem.
> >
> > I'm not following here. How exactly would you find symbols like this with coccinelle?
> >
> > subsystem.h:
> >
> > extern int subsystem_state;
> >
> > subsystem.c:
> >
> > #include "subsystem.h"
> > long subsystem_state;
> >
> > static void function(void)
> > {
> > 	...
> > 	subsystem_state++;
> > 	...
> > }
> >
> > with no users of this variable outside of this file.
> 
> The following uses ocaml, because I'm not very good at python...
> 
> @r disable optional_storage@
> identifier f;
> position p;
> @@
> 
> f@p(...) { ... }
> 
> @script:ocaml@
> p << r.p;
> f << r.f;
> @@
> 
> let l = Common,cmd_to_list
>           (Printf.sprintf "cd /my/linux/path; git grep %s" f) in
> let other_files =
>   List.map (function x -> List.hd(Str.split (Str.regexp ":") x)) l in
> let file = (List.hd p).file in
> match List.filter (function x -> not(x = file)) other_files with
>   [] -> Printf.printf "function %s is only used in file %s\n" f file
> | _ -> ()
> 
> That is, find the definition of a function with the pattern matching rule,
> then obtain the name and position of the function definition, then in the
> ocaml/python rule, grep for the name of the function, and extract the
> names of the files from grep.  Drop all the names that are the same as
> that of the file containing the definition, and print an error message if
> the result is an empty list.
> 
> The disable optional_storage at the beginning of the first rule prevents
> the function definition from matching a static function, since static
> isn't mentioned in the pattern.
> 
> This takes care of the case of function definitions.  Something similar
> could probably be done for other kinds fo variables.  That could be a bit
> more awkward, though because variable declarations both inside and outside
> function definitions look alike.

Ok, I see. Yes, this looks nice, though there might still be a small number
of false positives (e.g. identifier used in inline function in the header
that declares it, inline function used elsewhere) and false negatives
(function name mentioned in a .c file comment elsewhere but not actually
called).

I don't actually understand half the code you write, but conceptually
it makes sense and is similar to what I did manually. If you plan to
actually make use of this method, there are a couple of improvements
that I can think of:

- "git grep -w" to match only whole words
- "git grep -l" to list the file names only, rather than manually parsing
  the default grep output
- filter out declarations from header files as you mentioned (I don't
  see this in your code).

	Arnd


  reply	other threads:[~2015-10-01 20:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-01  9:37 [PATCH 0/2] Declare functions and structures as static Shraddha Barke
2015-10-01  9:37 ` [PATCH 1/2] Staging: lustre: ptlrpc: Make functions static Shraddha Barke
2015-10-01 10:03   ` [Outreachy kernel] " Arnd Bergmann
2015-10-01 10:05     ` Shraddha Barke
2015-10-01 13:20       ` Julia Lawall
2015-10-01 13:34         ` Shraddha Barke
2015-10-01 13:37         ` Arnd Bergmann
2015-10-01 13:49           ` Julia Lawall
2015-10-01 13:57             ` Arnd Bergmann
2015-10-01 14:12               ` Julia Lawall
2015-10-01 20:53                 ` Arnd Bergmann [this message]
2015-10-01 21:21                   ` Julia Lawall
2015-10-01 11:54   ` Sudip Mukherjee
2015-10-01  9:37 ` [PATCH 2/2] Staging: lustre: obdclass: Declare structure as static Shraddha Barke
2015-10-01  9:54   ` [Outreachy kernel] " Arnd Bergmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18050482.hzx1IbEyMT@wuerfel \
    --to=arnd@arndb.de \
    --cc=julia.lawall@lip6.fr \
    --cc=outreachy-kernel@googlegroups.com \
    --cc=shraddha.6596@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.