corrode
corrode copied to clipboard
Corrode has trouble with "implicit ints" in K&R style
I guess this is what I get for trying to modernise C code that turned 25 this year. 😄
$ cat test_pass.c
#include <stdio.h>
int
main(argc, argv)
int argc;
char **argv;
{
printf("Hello world!\n");
return 0;
}
$ corrode test_pass.c
<command-line>:0:1: warning: undefining "__LINE__"
test_pass.rs
test_pass.o
$ cat test_fail.c
#include <stdio.h>
int
main(argc, argv)
char **argv; // argc is implicitly type int
{
printf("Hello world!\n");
return 0;
}
$ corrode test_fail.c
<command-line>:0:1: warning: undefining "__LINE__"
("test_fail.c": line 4): illegal undeclared argument "argc"; check whether a real C compiler accepts this:
main(argc, argv)
$ gcc -c test_fail.c
test_fail.c: In function ‘main’:
test_fail.c:4:1: warning: type of ‘argc’ defaults to ‘int’ [-Wimplicit-int]
main(argc, argv)
^~~~
While this example may seem simple, are a lot of register foo
declarations scattered throughout the code, as well as some number of function extern definitions that are weird (that I may open a separate issue for if I can better characterize it).
Maybe this is easy to fix?
What are the rules for implicit ints? Will any argument without a type signature be inferred as an int?
My code infers the int type (and only int, as far as I can tell) in two instances: K&R type signature omission (as above), and variable declarations if another declaration keyword is used.
An example of the latter comes from the original K&R book, emphasis mine:
Section 4.7 Register Variables
The fourth and final storage class is called register. A register declaration advises the compiler that the variable in question will be heavily used. When possible, register variables are placed in machine registers, which may result in smaller and faster programs.
The register declaration looks like:
register int x;
register char c;
and so on; the int part may be omitted.
In other words, because register x;
is unambiguously a variable declaration, its type can be inferred as int. I think I saw an instance of static x;
in there too.