[clang] b9ec684 - Correctly diagnose incomplete arrays with static storage in C (#134374)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 7 04:12:36 PDT 2025
Author: Aaron Ballman
Date: 2025-04-07T07:12:33-04:00
New Revision: b9ec68431b45a9859517aacac684e7290f1679f2
URL: https://github.com/llvm/llvm-project/commit/b9ec68431b45a9859517aacac684e7290f1679f2
DIFF: https://github.com/llvm/llvm-project/commit/b9ec68431b45a9859517aacac684e7290f1679f2.diff
LOG: Correctly diagnose incomplete arrays with static storage in C (#134374)
A file scope declaration without an initializer which is neither extern
nor thread_local is a tentative definition. If the declaration of an
identifier for an object is a tentative definition and has internal
linkage, the declared type shall not be an incomplete type.
Clang was previously failing to diagnose this in -pedantic mode.
Fixes #50661
---------
Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva at intel.com>
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDecl.cpp
clang/test/C/drs/dr0xx.c
clang/test/Sema/incomplete-decl.c
clang/test/Sema/tentative-decls.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5217e04b5e83f..4d7a09e89ae42 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 393bfecf9a36b..c2b01833a5c46 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7350,8 +7350,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">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d630f9bd409fd..540f5f23fe89a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -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.
@@ -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());
}
}
diff --git a/clang/test/C/drs/dr0xx.c b/clang/test/C/drs/dr0xx.c
index 252dc9329c4ca..5fe023deaece9 100644
--- a/clang/test/C/drs/dr0xx.c
+++ b/clang/test/C/drs/dr0xx.c
@@ -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[];
{
diff --git a/clang/test/Sema/incomplete-decl.c b/clang/test/Sema/incomplete-decl.c
index bf2890bba9911..f8f234c6b7828 100644
--- a/clang/test/Sema/incomplete-decl.c
+++ b/clang/test/Sema/incomplete-decl.c
@@ -3,7 +3,7 @@
-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'}}
@@ -11,8 +11,7 @@ struct foo f; // c-error {{tentative definition has type 'struct foo' that is ne
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'}} \
- 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'}}
diff --git a/clang/test/Sema/tentative-decls.c b/clang/test/Sema/tentative-decls.c
index 713e65f3d9b35..94d21bdbf94da 100644
--- a/clang/test/Sema/tentative-decls.c
+++ b/clang/test/Sema/tentative-decls.c
@@ -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'}}
@@ -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 };
More information about the cfe-commits
mailing list