[comp.unix.questions] Checking # of parms

tad@cathedral.cerc.wvu.wvnet.edu (Tad Alan Davis) (12/21/89)

Is there a quick & dirty way, using grep, awk, or some such filter to check
the number of parmeters of a function.  Basically, I have a large set of 
functions which I call frequently with the wrong number of parms.  I want to
put the names of the functions and the number of parms in a file and use a
filter to check them in a C program.

For example	File	
			funcX 2

		Command	
			grep funcX prog.c | grep -v funcX([^,]*,[^,]*)

This works fine for

		funcX("a", "b");		not printed, valid
		funcX("a", "b", "c");		printed, invalid

      but not for

		funcX("a", funcX("a", "b"), "c");	not printed, invalid
		funcX("ok,ok", "a");			printed, valid


Any ideas which would only take an hour or two?

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (12/21/89)

In article <339@cerc.wvu.wvnet.edu.edu> tad@cathedral.cerc.wvu.wvnet.edu (Tad Alan Davis) writes:
: Is there a quick & dirty way, using grep, awk, or some such filter to check
: the number of parmeters of a function.  Basically, I have a large set of 
: functions which I call frequently with the wrong number of parms.  I want to
: put the names of the functions and the number of parms in a file and use a
: filter to check them in a C program.
: 
: For example	File	
: 			funcX 2
: 
: 		Command	
: 			grep funcX prog.c | grep -v funcX([^,]*,[^,]*)
: 
: This works fine for
: 
: 		funcX("a", "b");		not printed, valid
: 		funcX("a", "b", "c");		printed, invalid
: 
:       but not for
: 
: 		funcX("a", funcX("a", "b"), "c");	not printed, invalid
: 		funcX("ok,ok", "a");			printed, valid
: 
: 
: Any ideas which would only take an hour or two?

How about a minute or two.  Put your definitions into File as

	#define funcX(a,b)

Then do something like

	cat File prog.c | /lib/cpp >/dev/null

or whatever you need to do to feed your files to the C preprocessor.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

cpcahil@virtech.uucp (Conor P. Cahill) (12/21/89)

In article <339@cerc.wvu.wvnet.edu.edu>, tad@cathedral.cerc.wvu.wvnet.edu (Tad Alan Davis) writes:
> 
> Is there a quick & dirty way, using grep, awk, or some such filter to check
> the number of parmeters of a function.  Basically, I have a large set of 
> functions which I call frequently with the wrong number of parms.  I want to
> put the names of the functions and the number of parms in a file and use a
> filter to check them in a C program.

why not just use cpp.

> 
> For example	File	
> 			funcX 2
> 
> 		Command	
> 			grep funcX prog.c | grep -v funcX([^,]*,[^,]*)

		File

			funcX 2

		Commands:
			echo "#define funcX(a,b) never_used_name1(a,b)" > t$$.c
			cat prog.c > t$$.c
			cc -P t$$.c

			/* you will get an argument mismatch message for 
			   each incorrect usage.  You only have to adjust for 
			   the extra #defines you added to the beginning
			   of the file.


This will work for all the examples you gave.
If you are really worried about this as an ongoing problem, you might
want to put the following into an include file:

	#ifdef CHECK_FUNC_ARGS
	#define funcX(a,b) never_used_name1(a,b)"
	#define funcY(a,b,c) never_used_name2(a,b)"
	.
	.
	.
	#endif

and include it in your modules.  That way you could just add a
-DCHECK_FUNC_ARGS to the compile line whenever you wanted to check
the argument counts.  

I wouldn't use the module compiled with this flag turned on because it
will make debugging real hard, since the linker will see the 
function names "never_used_name1, etc" and not funcX/funcY.

Good luck


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

gwc@root.co.uk (Geoff Clare) (12/22/89)

In article <339@cerc.wvu.wvnet.edu.edu> tad@cathedral.cerc.wvu.wvnet.edu (Tad Alan Davis) writes:

>Is there a quick & dirty way, using grep, awk, or some such filter to check
>the number of parmeters of a function.  Basically, I have a large set of 
>functions which I call frequently with the wrong number of parms.  I want to
>put the names of the functions and the number of parms in a file and use a
>filter to check them in a C program.

I have seen a couple of answers to this which suggest using the
C preprocessor for this.  That's fine as far as it goes, but I would
suggest a better solution is to create a lint library for these
functions, then you will get the argument types checked as well.
-- 
Geoff Clare, UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH
gwc@root.co.uk  (Dumb mailers: ...!uunet!root.co.uk!gwc)  Tel: +44-1-315-6600