From mboxrd@z Thu Jan 1 00:00:00 1970 From: mstefani@redhat.com (Michael Stefaniuc) Date: Wed, 5 Oct 2016 18:39:25 +0200 Subject: [Cocci] Replacing one (specific!) type with another In-Reply-To: <87shsa7p71.fsf@thinkpad.rath.org> References: <87r37vzd8j.fsf@vostro.rath.org> <87shsa7p71.fsf@thinkpad.rath.org> Message-ID: <57F52CBD.7020204@redhat.com> To: cocci@systeme.lip6.fr List-Id: cocci@systeme.lip6.fr On 10/05/2016 06:09 PM, Nikolaus Rath wrote: > On Oct 05 2016, Julia Lawall wrote: >> On Tue, 4 Oct 2016, Nikolaus Rath wrote: >> >>> Hello, >>> >>> In a set of C files, I would like to replace each instance of one type >>> (struct fasel_foo) with another type (struct fasel_bar). For instance, >>> >>> char* fasel_foo_print(struct fasel_foo *ptr) { >>> // ... >>> return "this struct fasel_foo string should not change"; >>> } >>> >>> should become >>> >>> char* fasel_foo_print(struct fasel_bar *ptr) { >>> // ... >>> return "this struct fasel_foo string should not change"; >>> } >>> >>> >>> Based on the LWN articles that I've read, this seems like the perfect >>> use-case for coccinelle. However, somehow I'm struggling to write a >>> patch for this. All the documentation that I could get my hands on seems >>> to describe more abstract changes that require the use of variables - >>> but as far as I can tell, I need something much simpler: >>> >>> @@ >>> "struct fasel_foo must be a type name!" >>> @@ >>> - struct fasel_foo >>> + struct fasel_bar >>> >>> >>> ...if only I knew what to put between the @@. >> >> Put nothing :) From the word struct it should figure out that it is >> working on a type. > > Oh, great! Thanks! > > > This solves my problem, but it makes me wonder how this generalizes to > other problems. For example, > > 1. What would I need to do if I don't want to replace a struct but > something typedef'd? (my_type *ptr --> new_type *ptr). Easy @@ typedef my_type, new_type; @@ - my_type + new_type > 2. ..and how would I go about if instead of the type, I want to replace > a variable name? (my_type *ptr --> my_type *pointer). Depends on the exact circumstance. But that's easy to figure out. I recommend the workshop papers and tutorials from http://coccinelle.lip6.fr/papers.php; I wish I would have had those when I started with coccinelle. bye michael