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

fix issue #70 about storage class v2 #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tstreiff
Copy link
Contributor

@tstreiff tstreiff commented Jun 8, 2020

No description provided.

@tstreiff
Copy link
Contributor Author

Could we discuss this?
I do not manage to interpret the failure in the report.

This bug is annoying because without fix, using a library variable like "errno" is not possible.
The first "extern int errno;" in an include file prevents the generation of the variable in an object file.

@windelbouwman
Copy link
Owner

Yes, we should discuss this! I was examining the change, and I realized I did not understand it fully, will have a close look at it. I agree that this is very annoying, since this is a very common pattern.

@@ -320,17 +320,21 @@ def check_redeclaration_storage_class(self, sym, declaration):
old_storage_class = sym.declaration.storage_class
new_storage_class = declaration.storage_class
# None == automatic storage class.
invalid_combos = [(None, "static"), ("extern", "static")]
# changes of linkage between internal and external are illegal
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Playing around a bit at this site makes it appear differently, you can declare a function as non-static after first specifying it as static.

declaration.storage_class = sym.declaration.storage_class
# if new storage-class is "extern", keep the old storage-class
# otherwise use the new one
if new_storage_class == "extern":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than changing the storage class, it is probably better to change the position in the Symbol.declarations list of this declaration.

invalid_combos = [(None, "static"), ("extern", "static")]
# changes of linkage between internal and external are illegal
invalid_combos = [(None, "static"),
("extern", "static"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this code a bit, so that there are booleans checking for previously static or not.

@tstreiff
Copy link
Contributor Author

tstreiff commented Jun 22, 2020

Much better. It solves the "errno" issue.

I tried to test most combinations and there are 2 small problems remaining:

static int i4; // internal linkage
extern int i4; // keep internal linkage
int i4; // illegal: try to change linkage to external

Current ppci-cc accpts the 3rd line whereas it should not since it is an attempt to change linkage.
(gcc reports an error: "error: non-static declaration of ‘i4’ follows static declaration")

static void f(); // internal linkage
void void f(); // legal because for functions, no storage specifier means implicitly "extern"

Current ppci-cc correctly accepts the 2nd line which is correct, but it changes the linkage from "internal" to "external" whereas the linkage should be kept "internal" (the function "f" is declared global in the object file.
There is a subtle difference beween variables and functions there.

The C standard states:

"If the declaration of an identifier for a function has no storage-class specifier, its linkage
is determined exactly as if it were declared with the storage-class specifier extern."

and:

"For an identifier declared with the storage-class specifier extern in a scope in which a
prior declaration of that identifier is visible, if the prior declaration specifies internal or
external linkage, the linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration."

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

Successfully merging this pull request may close these issues.

None yet

2 participants