Skip to content

Confusing diagnostics: error: 'extern' variable cannot have an initializer #57478

Closed
@pmor13

Description

@pmor13

This C code:

static int x;

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

leads to:

error: 'extern' variable cannot have an initializer

which is confusing, because extern variable CAN have an initializer, which is demonstrated by the C standard (C11, 6.9.2 External object definitions, 4, EXAMPLE 1):

extern int i3 = 3; // definition, external linkage

The expected diagnostics is:

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

Relevant quote from C11:

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.

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerenhancementImproving things as opposed to bug fixing, e.g. new or missing featuregood first issuehttps://github.com/llvm/llvm-project/contribute

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions