Skip to content

Correctly diagnose incomplete arrays with static storage in C #134374

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

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ Improvements to Clang's diagnostics
- ``-Wc++98-compat`` no longer diagnoses use of ``__auto_type`` or
``decltype(auto)`` as though it was the extension for ``auto``. (#GH47900)

- Now correctly diagnose a tentative definition of an array with static
storage duration in pedantic mode in C. (#GH50661)

Improvements to Clang's time-trace
----------------------------------

Expand Down
5 changes: 3 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -7346,8 +7346,9 @@ def err_typecheck_pointer_arith_void_type : Error<
"arithmetic on%select{ a|}0 pointer%select{|s}0 to void">;
def err_typecheck_decl_incomplete_type : Error<
"variable has incomplete type %0">;
def ext_typecheck_decl_incomplete_type : ExtWarn<
"tentative definition of variable with internal linkage has incomplete non-array type %0">,
def ext_typecheck_decl_incomplete_type : Extension<
"tentative definition of variable with internal linkage has incomplete "
"%select{non-array|array}0 type %1">,
InGroup<DiagGroup<"tentative-definition-incomplete-type">>;
def err_tentative_def_incomplete_type : Error<
"tentative definition has type %0 that is never completed">;
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14246,7 +14246,8 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
Var->getLocation(), ArrayT->getElementType(),
diag::err_array_incomplete_or_sizeless_type))
Var->setInvalidDecl();
} else if (Var->getStorageClass() == SC_Static) {
}
if (Var->getStorageClass() == SC_Static) {
// C99 6.9.2p3: If the declaration of an identifier for an object is
// a tentative definition and has internal linkage (C99 6.2.2p3), the
// declared type shall not be an incomplete type.
Expand All @@ -14258,7 +14259,8 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
// NOTE: to avoid multiple warnings, only check the first declaration.
if (Var->isFirstDecl())
RequireCompleteType(Var->getLocation(), Type,
diag::ext_typecheck_decl_incomplete_type);
diag::ext_typecheck_decl_incomplete_type,
Type->isArrayType());
}
}

Expand Down
4 changes: 3 additions & 1 deletion clang/test/C/drs/dr0xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ int dr010_c = sizeof(dr010_t); /* expected-error {{invalid application of 'sizeo
* Note: DR034 has a question resolved by DR011 and another question where the
* result is UB.
*/
static int dr011_a[]; /* expected-warning {{tentative array definition assumed to have one element}} */
static int dr011_a[]; /* expected-warning {{tentative array definition assumed to have one element}}
expected-warning {{tentative definition of variable with internal linkage has incomplete array type 'int[]'}}
*/
void dr011(void) {
extern int i[];
{
Expand Down
5 changes: 2 additions & 3 deletions clang/test/Sema/incomplete-decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@



struct foo; // c-note 5 {{forward declaration of 'struct foo'}} \
struct foo; // c-note 4 {{forward declaration of 'struct foo'}} \
cxx-note 3 {{forward declaration of 'foo'}}

void b; // expected-error {{variable has incomplete type 'void'}}
struct foo f; // c-error {{tentative definition has type 'struct foo' that is never completed}} \
cxx-error {{variable has incomplete type 'struct foo'}}

static void c; // expected-error {{variable has incomplete type 'void'}}
static struct foo g; // c-warning {{tentative definition of variable with internal linkage has incomplete non-array type 'struct foo'}} \
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did this one disappear?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because the diagnostic went from ExtWarn to Extension so it's no longer emitted by default. There's nothing dangerous about the tentative definition by itself, and we give other diagnostics in cases where it is an issue (tentative definition has type 'struct foo' that is never completed or tentative array definition assumed to have one element).

c-error {{tentative definition has type 'struct foo' that is never completed}} \
static struct foo g; // c-error {{tentative definition has type 'struct foo' that is never completed}} \
cxx-error {{variable has incomplete type 'struct foo'}}

extern void d; // cxx-error {{variable has incomplete type 'void'}}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Sema/tentative-decls.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 %s -fsyntax-only -Wprivate-extern -verify
// RUN: %clang_cc1 %s -fsyntax-only -Wprivate-extern -pedantic -verify

// PR3310
struct a x1; // expected-note 2{{forward declaration of 'struct a'}}
Expand Down Expand Up @@ -58,7 +58,7 @@ void func2(void)
extern double *p;
}

static int a0[];
static int a0[]; // expected-warning {{tentative definition of variable with internal linkage has incomplete array type 'int[]'}}
static int b0;

static int a0[] = { 4 };
Expand Down