This is an archive of the discontinued LLVM Phabricator instance.

Update declaration message of extern linkage
Needs ReviewPublic

Authored by Krishna-13-cyber on Apr 9 2023, 11:42 AM.

Details

Summary

This patch aims to improve the diagnostic to much better precision of indicating extern declaration being followed after static declaration rather than just generic non-static declaration.

Example Testcase: https://godbolt.org/z/zMTda6MGG

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptApr 9 2023, 11:42 AM
Krishna-13-cyber requested review of this revision.Apr 9 2023, 11:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 9 2023, 11:42 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder added inline comments.Apr 12 2023, 6:41 AM
clang/test/SemaCXX/extern_static.cpp
2

That's unnecessary, isn't it?

  • Updated with removing redundant flags
cjdb added a comment.Apr 17 2023, 11:00 AM

I think we should fundamentally rethink this entire category of diagnostic. Rather than having a static diagnostic per offence stating what happened, we should instead have a single diagnostic that captures both what happened, why it's bad, and how to fix it. Something along the lines of

error: 'x' has been declared with incompatible linkage specifiers (static and extern); please pick exactly one
  extern int x;
  ^~~~~~
note: previous definition here
  static int x;
  ^~~~~~

It'd also be more robust from an engineering perspective, since it means that we won't need to add new diagnostic permutations every time a new linkage specifier is added.

I think we should fundamentally rethink this entire category of diagnostic. Rather than having a static diagnostic per offence stating what happened, we should instead have a single diagnostic that captures both what happened, why it's bad, and how to fix it. Something along the lines of

error: 'x' has been declared with incompatible linkage specifiers (static and extern); please pick exactly one
  extern int x;
  ^~~~~~
note: previous definition here
  static int x;
  ^~~~~~

It'd also be more robust from an engineering perspective, since it means that we won't need to add new diagnostic permutations every time a new linkage specifier is added.

I think we should fundamentally rethink this entire category of diagnostic. Rather than having a static diagnostic per offence stating what happened, we should instead have a single diagnostic that captures both what happened, why it's bad, and how to fix it. Something along the lines of

error: 'x' has been declared with incompatible linkage specifiers (static and extern); please pick exactly one
  extern int x;
  ^~~~~~
note: previous definition here
  static int x;
  ^~~~~~

It'd also be more robust from an engineering perspective, since it means that we won't need to add new diagnostic permutations every time a new linkage specifier is added.

I will try giving this a shot
Thanks!

Krishna-13-cyber updated this revision to Diff 515843.EditedApr 21 2023, 11:32 AM

I have tried a little modification from my side thinking on a beneficial note. I will make the changes to all the other test files as well if this diagnostic representation goes well after mentor review.

For refactoring and restructuring the whole of the re-declaration would need some time I have been through it and would initiate another patch for that,In the concern of giving or having just one diagnostic for getting all cases of re-declaration would also need multiple conditional or switch statements inside our function block.At present we have the same with conditional statements taking care of each linkage but it has multiple permutations of diagnostic messages which is nice but can be improved.GCC differs only for this case of extern linkage which can be better/precise in clang where as others seem to be more precise in clang than former as I worked out with good number of test cases regarding this.

I have tried a little modification from my side thinking on a beneficial note. I will make the changes to all the other test files as well if this diagnostic representation goes well after mentor review.

For refactoring and restructuring the whole of the re-declaration would need some time I have been through it and would initiate another patch for that,In the concern of giving or having just one diagnostic for getting all cases of re-declaration would also need multiple conditional or switch statements inside our function block.At present we have the same with conditional statements taking care of each linkage but it has multiple permutations of diagnostic messages which is nice but can be improved.GCC differs only for this case of extern linkage which can be better/precise in clang where as others seem to be more precise in clang than former as I worked out with good number of test cases regarding this.

Ping!

Krishna-13-cyber added a comment.EditedJun 30 2023, 7:04 AM

Ping.

Thanks for the ping! Does this require any further modification?
I think we will have to change the whole set of diagnostics if we go with this change of pick exactly one convention.

I think the existing wording is pretty reasonable, changing "non-static declaration" into "extern declaration" isn't really giving the user any more information to help them resolve the issue. "please pick exactly one" doesn't seem likely to help the user either -- they already know there's a conflict, so picking one is already the solution the user is likely to have in mind. The hard part for the user is with figuring which one to pick, but we have no way to help them make that choice. So I'm not certain changes are needed in this space (I'm not opposed either), but I do think the idea @cjdb had to combine all these diagnostics into one using %select could be helpful. However, there are enough options that it could also be pretty complex to reason about. There's static, extern, and thread-local, as well as "non-" versions of each of those, so there are quite a few combinations.

I think the existing wording is pretty reasonable, changing "non-static declaration" into "extern declaration" isn't really giving the user any more information to help them resolve the issue. "please pick exactly one" doesn't seem likely to help the user either -- they already know there's a conflict, so picking one is already the solution the user is likely to have in mind. The hard part for the user is with figuring which one to pick, but we have no way to help them make that choice. So I'm not certain changes are needed in this space (I'm not opposed either), but I do think the idea @cjdb had to combine all these diagnostics into one using %select could be helpful. However, there are enough options that it could also be pretty complex to reason about. There's static, extern, and thread-local, as well as "non-" versions of each of those, so there are quite a few combinations.

Yes I agree with this,
Changing the whole set of diagnostics will be quite challenging, but I think extern has an edge over non-static declaration when we are able to see the diagnostics in gcc if are not going with the pick-exactly one convention.

I think the existing wording is pretty reasonable, changing "non-static declaration" into "extern declaration" isn't really giving the user any more information to help them resolve the issue. "please pick exactly one" doesn't seem likely to help the user either -- they already know there's a conflict, so picking one is already the solution the user is likely to have in mind. The hard part for the user is with figuring which one to pick, but we have no way to help them make that choice. So I'm not certain changes are needed in this space (I'm not opposed either), but I do think the idea @cjdb had to combine all these diagnostics into one using %select could be helpful. However, there are enough options that it could also be pretty complex to reason about. There's static, extern, and thread-local, as well as "non-" versions of each of those, so there are quite a few combinations.

Yes I agree with this,
Changing the whole set of diagnostics will be quite challenging, but I think extern has an edge over non-static declaration when we are able to see the diagnostics in gcc if are not going with the pick-exactly one convention.

I can sort of buy the argument that extern has a slight edge, but I think what would really help are test cases for all the various combinations so we can see them at once (static, extern. no linkage, thread local vs each counterpart). I'm concerned that the code you added conflicts with the existing code immediately below your changes and the standards wording quoted above your changes, but I left a comment on what would make me more comfortable.

clang/lib/Sema/SemaDecl.cpp
4641–4657
clang/test/SemaCXX/extern_static.cpp
7

Be sure to add the newline back.

Also, I think there should be a C run line for the test.