public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts: add typdef removal tool
@ 2009-08-11 22:51 Luis R. Rodriguez
  2009-08-11 23:34 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Luis R. Rodriguez @ 2009-08-11 22:51 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, devel, joe, Luis R. Rodriguez

If you are porting drivers and need to remove tydpefs you can use
this to help you port the driver quicker.

Cc: Joe Perches <joe@perches.com>
Signed-off-by: Luis R. Rodriguez <lrodriguez@atheros.com>
---
 scripts/remove-typedefs |   65 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 0 deletions(-)
 create mode 100755 scripts/remove-typedefs

diff --git a/scripts/remove-typedefs b/scripts/remove-typedefs
new file mode 100755
index 0000000..ae6952c
--- /dev/null
+++ b/scripts/remove-typedefs
@@ -0,0 +1,65 @@
+#!/bin/bash
+#
+# Copyright 2009 Joe Perches <joe@perches.com>
+# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
+#
+# Lets you remove typdefs from C/header files.
+#
+# We do simple sed substituation for logical places where you would use the typdef,
+# and also replace the typdef declaration to a simple struct declaration.
+
+function usage()
+{
+	echo "Usage $0 <typdef-name> <struct-name> <paths>"
+	exit 1
+}
+
+function remove_typedef()
+{
+	if [ ! -f $1 ]; then
+		return;
+	fi
+
+	# This replaces the typdef declaration for a simple struct declaration - style 1
+	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+_$from\s*\{([\d\D]+?)\}\s*struct\s+$to\b[^;]*;/struct $to \{\1\};/g; print; }" $1
+
+	# This replaces the typdef declaration for a simple struct declaration - style 2
+	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+$to\s*\{([\d\D]+?)\}\s*$from\b[^;]*;/struct $to \{\1\};/g; print; }" $1
+
+	# This replaces the typdef usages with simple structs
+	sed -r -i -e "s/\b$from\b/struct $to/g" $1
+	sed -r -i -e "s/\bP$from\b/struct $to \*/g" $1
+	sed -r -i -e "s/struct $to\s*\*\s*\b/struct $to \*/g" $1
+	sed -r -i -e "s/\(struct $to\s*\*\)\s*/\(struct $to \*\)/g" $1
+}
+
+if [[ $# -lt 3 ]]; then
+	usage
+fi
+
+from=$1
+to=$2
+shift
+
+echo "Converting typedef $from to struct $to"
+
+while shift; do
+	REM_PATH=$1
+
+	if [ -z $REM_PATH ]; then
+		continue;
+	fi
+
+	if [ ! -d $REM_PATH ]; then
+		echo "No directory $REM_PATH";
+		continue;
+	fi
+
+	for i in $(find $REM_PATH -type f -name *.c); do
+		remove_typedef $i
+	done
+
+	for i in $(find $REM_PATH -type f -name *.h); do
+		remove_typedef $i
+	done
+done
-- 
1.6.3.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] scripts: add typdef removal tool
  2009-08-11 22:51 [PATCH] scripts: add typdef removal tool Luis R. Rodriguez
@ 2009-08-11 23:34 ` Joe Perches
  2009-08-14  5:29   ` Luis R. Rodriguez
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2009-08-11 23:34 UTC (permalink / raw)
  To: Luis R. Rodriguez; +Cc: akpm, linux-kernel, devel

On Tue, 2009-08-11 at 15:51 -0700, Luis R. Rodriguez wrote:
> If you are porting drivers and need to remove tydpefs you can use
> this to help you port the driver quicker.

> +	# This replaces the typdef declaration for a simple struct declaration - style 1
> +	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+_$from\s*\{([\d\D]+?)\}\s*struct\s+$to\b[^;]*;/struct $to \{\1\};/g; print; }" $1
> +
> +	# This replaces the typdef declaration for a simple struct declaration - style 2
> +	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+$to\s*\{([\d\D]+?)\}\s*$from\b[^;]*;/struct $to \{\1\};/g; print; }" $1
> +
> +	# This replaces the typdef usages with simple structs
> +	sed -r -i -e "s/\b$from\b/struct $to/g" $1
> +	sed -r -i -e "s/\bP$from\b/struct $to \*/g" $1
> +	sed -r -i -e "s/struct $to\s*\*\s*\b/struct $to \*/g" $1
> +	sed -r -i -e "s/\(struct $to\s*\*\)\s*/\(struct $to \*\)/g" $1

Hi Luis.

A couple of comments:

In the comments "typdef" should be "typedef".

I believe the sed statements need to be run before the perl statements.

Another common microsoft form is

	typedef struct tagFOO {
		members
	} FOO [, *PFOO [, **PPFOO ];

So maybe perl statement 1 could be:

s/\btypedef\s+struct\s+$to\s*(_{0,1}|tag)$from etc

and another sed statement might be:

sed -r -i -e "s/\(struct $to\s*\*\s*\*\s*\)\s*/\(struct $to \*\*\)/g" $1

Maybe you could test this against next:drivers/staging/hv/ to see
how well this works.

cheers, Joe


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scripts: add typdef removal tool
  2009-08-11 23:34 ` Joe Perches
@ 2009-08-14  5:29   ` Luis R. Rodriguez
  0 siblings, 0 replies; 3+ messages in thread
From: Luis R. Rodriguez @ 2009-08-14  5:29 UTC (permalink / raw)
  To: Joe Perches
  Cc: Luis Rodriguez, akpm@linux-foundation.org,
	linux-kernel@vger.kernel.org, devel@driverdev.osuosl.org

On Tue, Aug 11, 2009 at 04:34:15PM -0700, Joe Perches wrote:
> On Tue, 2009-08-11 at 15:51 -0700, Luis R. Rodriguez wrote:
> > If you are porting drivers and need to remove tydpefs you can use
> > this to help you port the driver quicker.
> 
> > +     # This replaces the typdef declaration for a simple struct declaration - style 1
> > +     perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+_$from\s*\{([\d\D]+?)\}\s*struct\s+$to\b[^;]*;/struct $to \{\1\};/g; print; }" $1
> > +
> > +     # This replaces the typdef declaration for a simple struct declaration - style 2
> > +     perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+$to\s*\{([\d\D]+?)\}\s*$from\b[^;]*;/struct $to \{\1\};/g; print; }" $1
> > +
> > +     # This replaces the typdef usages with simple structs
> > +     sed -r -i -e "s/\b$from\b/struct $to/g" $1
> > +     sed -r -i -e "s/\bP$from\b/struct $to \*/g" $1
> > +     sed -r -i -e "s/struct $to\s*\*\s*\b/struct $to \*/g" $1
> > +     sed -r -i -e "s/\(struct $to\s*\*\)\s*/\(struct $to \*\)/g" $1
> 
> Hi Luis.
> 
> A couple of comments:
> 
> In the comments "typdef" should be "typedef".

Fixed.

> I believe the sed statements need to be run before the perl statements.

OK, swapped it around.

> Another common microsoft form is
> 
>         typedef struct tagFOO {
>                 members
>         } FOO [, *PFOO [, **PPFOO ];
> 
> So maybe perl statement 1 could be:
> 
> s/\btypedef\s+struct\s+$to\s*(_{0,1}|tag)$from etc
> 
> and another sed statement might be:
> 
> sed -r -i -e "s/\(struct $to\s*\*\s*\*\s*\)\s*/\(struct $to \*\*\)/g" $1

Yeah I just spotted another form:

typedef struct whatever {
	members
} FOO;

So we need to ignore 'whatever', I've tested this. I'll send a v2 with this and
addressing the other comments you've made.

Mind sending these as a patch on top of the script?

> Maybe you could test this against next:drivers/staging/hv/ to see
> how well this works.

I'm testing against some ar9271 driver I'm porting but I get a chance
I'll test it hv, not sure if I will though. Feel free to do so though
and let me know how it goes.

  Luis

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-08-14  5:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-11 22:51 [PATCH] scripts: add typdef removal tool Luis R. Rodriguez
2009-08-11 23:34 ` Joe Perches
2009-08-14  5:29   ` Luis R. Rodriguez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox