This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Reword diagnostic for scope identifier with linkage
ClosedPublic

Authored by junaire on Aug 31 2022, 9:11 PM.

Details

Reviewers
aaron.ballman
Summary

If the declaration of an identifier has block scope, and the identifier has
external or internal linkage, the declaration shall have no initializer for
the identifier.

Clang now gives a more suitable diagnosis for this case.
Fixes https://github.com/llvm/llvm-project/issues/57478

Signed-off-by: Jun Zhang <jun@junz.org>

Diff Detail

Event Timeline

junaire created this revision.Aug 31 2022, 9:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2022, 9:11 PM
junaire requested review of this revision.Aug 31 2022, 9:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2022, 9:11 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
junaire updated this revision to Diff 457171.Aug 31 2022, 9:32 PM

Simpify code a little bit.

pmor13 added a subscriber: pmor13.EditedSep 1 2022, 12:42 PM

Note that this code:

int x;

void f(void)
{
    extern int x = 1;
}

needs to produce:

error: declaration of block scope identifier with external linkage shall have no initializer

So, err_block_extern_cant_init needs to be updated too (I think).

junaire updated this revision to Diff 457485.Sep 1 2022, 8:05 PM

Update the existing diagnostic and its tests.

pmor13 added a comment.Sep 2 2022, 1:03 PM

Looks good.

gentle ping :)

aaron.ballman added inline comments.Sep 9 2022, 10:21 AM
clang/include/clang/Basic/DiagnosticSemaKinds.td
5904–5905

I don't think we want to try to call out external vs internal linkage here, more on why below. Also, we don't usually use "shall" in diagnostic wording.

clang/lib/Sema/SemaDecl.cpp
12776–12786
12781–12782
clang/test/Sema/err-decl-block-extern-no-init.c
7

This example is why I think we should reword the diagnostic. As it stands, this diagnostic wording would be baffling to most users because they'd go "whaddya mean *INTERNAL* linkage, I said extern for a reason!" So using a diagnostic wording of "with linkage" sidesteps that -- what the linkage is doesn't matter for fixing the code, it's the initializer that's the issue.

Also, I don't know what it means for a block scope variable to have *internal* linkage instead of *no* linkage (and I'm not certain it's possible to observe the difference).

That said, I think this example should have a *second* diagnostic, which is a warning, letting the user know that the actual linkage does not match the specified linkage. (This should be done in a separate patch.)

@aaron.ballman

block scope variable to have *internal* linkage instead of *no* linkage

static int x;

void f(void)
{
    extern int x;  // block scope, internal linkage 
}

@aaron.ballman

block scope variable to have *internal* linkage instead of *no* linkage

static int x;

void f(void)
{
    extern int x;  // block scope, internal linkage 
}

No, I understood that, I meant in terms of the semantics. I'm not 100% convinced there's a way to *observe* the difference between no and internal linkage, but I *think* you might be able to observe it regarding an inline function returning the address of an internal linkage variable. If it's internal linkage, every copy of the function shares the same object but if it had no linkage, the linker wouldn't have to collapse them all down to the same object (maybe?).

junaire updated this revision to Diff 459287.Sep 10 2022, 7:06 AM

Try to address Aaron's comments.

junaire updated this revision to Diff 459394.Sep 11 2022, 7:46 PM

Rebase, adjust commit message and release note.

junaire retitled this revision from [Clang] Fix wrong diagnostic for scope identifier with internal linkage to [Clang] Reword diagnostic for scope identifier with internal linkage.Sep 11 2022, 7:47 PM
junaire edited the summary of this revision. (Show Details)
junaire retitled this revision from [Clang] Reword diagnostic for scope identifier with internal linkage to [Clang] Reword diagnostic for scope identifier with linkage.
aaron.ballman accepted this revision.Sep 12 2022, 5:37 AM

LGTM, thank you!

This revision is now accepted and ready to land.Sep 12 2022, 5:37 AM
junaire closed this revision.Sep 12 2022, 7:43 AM