Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrode has trouble with "implicit ints" in K&R style #169

Open
jhwgh1968 opened this issue Dec 10, 2017 · 2 comments
Open

Corrode has trouble with "implicit ints" in K&R style #169

jhwgh1968 opened this issue Dec 10, 2017 · 2 comments

Comments

@jhwgh1968
Copy link

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?

@phase
Copy link

phase commented Dec 27, 2017

What are the rules for implicit ints? Will any argument without a type signature be inferred as an int?

@jhwgh1968
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants