prototype mismatches

G

Gary

I recently joined a project with a mix of K&R C and ANSI C++. There are
instances
of prototype mismatches defined in one file and used in another that aren't
being caught and result in run time errors (see example below).

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.
Thanks in advance,
Gary

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

/********** file2.C ***************/
/* programmer introduces bug by declaring prototype
with one arg instead of two. */
extern "C" int add(int x);

int main(int argc, char* argv[])
{
int z = add(3); /* <-- incorrect signature not caught by linker */
return 0;
}
 
V

Victor Bazarov

Gary said:
I recently joined a project with a mix of K&R C and ANSI C++. There are
instances
of prototype mismatches defined in one file and used in another that aren't
being caught and result in run time errors (see example below).

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.
Thanks in advance,
Gary

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

/********** file2.C ***************/
/* programmer introduces bug by declaring prototype
with one arg instead of two. */
extern "C" int add(int x);

int main(int argc, char* argv[])
{
int z = add(3); /* <-- incorrect signature not caught by linker */
return 0;
}

First of all, the C++ program (the only relevant part of your post
here in comp.lang.c++) is _valid_. The fact that 'int add(int)' is
not defined should be caught by the linker and reported as error,
but there is no language problem since the prototype matches the use.

Second, the utility you're looking for is called "C++ compiler".
Take your K&R C code and copy into files with extension '.C' and run
them through the C++ compiler. Correct reported errors. Repeat
until no errors reported. Stuff the K&R code into an archive and
deposit it into a safe to never be seen again.

Victor
 
E

E. Robert Tisdale

Gary said:
I recently joined a project with a mix of K&R C and ANSI C++.
There are instances of prototype mismatches
defined in one file and used in another
that aren't being caught and result in run time errors.

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.



/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}
cat file1.h
/********** file1.h ***************/
/* declares a function with two args */
#ifndef GUARD_FILE1_H
#define GUARD_FILE1_H 1
#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
extern int add(int, int);
#ifdef __cplusplus
}
#endif/*__cplusplus */
#endif/*GUARD_FILE1_H 1 */
cat file1.1.c
#include "file1.h"
#include "file1.c"
gcc -Wall -std=c99 -pedantic -o file1.o -c file1.1.c
cat file2.C
/********** file2.C ***************/
/* programmer fixes bug
* by including header file. */

#include "file1.h"

int main(int argc, char* argv[]) {
int z = add(3);/* <-- incorrect signature caught by compiler*/
return 0;
}
g++ -Wall -ansi -pedantic -o file2 file2.C file1.o
file1.h: In function `int main(int, char**)':
file1.h:8: too few arguments to function `int add(int, int)'
file2.C:8: at this point in file
file2.C:8: warning: unused variable `int z'
 
G

Gary

Your sarcasm aside, you apparently missed reading that one of my constraints
is that my project manager won't permit large scale code changes. This is
legacy production code.

Gary

Victor Bazarov said:
Gary said:
I recently joined a project with a mix of K&R C and ANSI C++. There are
instances
of prototype mismatches defined in one file and used in another that aren't
being caught and result in run time errors (see example below).

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.
Thanks in advance,
Gary

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

/********** file2.C ***************/
/* programmer introduces bug by declaring prototype
with one arg instead of two. */
extern "C" int add(int x);

int main(int argc, char* argv[])
{
int z = add(3); /* <-- incorrect signature not caught by linker */
return 0;
}

First of all, the C++ program (the only relevant part of your post
here in comp.lang.c++) is _valid_. The fact that 'int add(int)' is
not defined should be caught by the linker and reported as error,
but there is no language problem since the prototype matches the use.

Second, the utility you're looking for is called "C++ compiler".
Take your K&R C code and copy into files with extension '.C' and run
them through the C++ compiler. Correct reported errors. Repeat
until no errors reported. Stuff the K&R code into an archive and
deposit it into a safe to never be seen again.

Victor
 
G

Gary

Thanks for the great suggestion. While I can't implement it directly since
I'm not allowed to change lots of production source code, it gave me other
ideas about autogenerating an alternate build directory.
Gary


E. Robert Tisdale said:
Gary said:
I recently joined a project with a mix of K&R C and ANSI C++.
There are instances of prototype mismatches
defined in one file and used in another
that aren't being caught and result in run time errors.

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.



/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}
cat file1.h
/********** file1.h ***************/
/* declares a function with two args */
#ifndef GUARD_FILE1_H
#define GUARD_FILE1_H 1
#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
extern int add(int, int);
#ifdef __cplusplus
}
#endif/*__cplusplus */
#endif/*GUARD_FILE1_H 1 */
cat file1.1.c
#include "file1.h"
#include "file1.c"
gcc -Wall -std=c99 -pedantic -o file1.o -c file1.1.c
cat file2.C
/********** file2.C ***************/
/* programmer fixes bug
* by including header file. */

#include "file1.h"

int main(int argc, char* argv[]) {
int z = add(3);/* <-- incorrect signature caught by compiler*/
return 0;
}
g++ -Wall -ansi -pedantic -o file2 file2.C file1.o
file1.h: In function `int main(int, char**)':
file1.h:8: too few arguments to function `int add(int, int)'
file2.C:8: at this point in file
file2.C:8: warning: unused variable `int z'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top