[clang] 97e0e67 - [clang][C23] Delay thread storage recovery for constexpr diagnostics (#236758)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 05:48:56 PST 2026


Author: Chen Miao
Date: 2026-11-11T21:48:51+08:00
New Revision: 97e0e67e311cbb4ddc68f14d20c585fed3769533

URL: https://github.com/llvm/llvm-project/commit/97e0e67e311cbb4ddc68f14d20c585fed3769533
DIFF: https://github.com/llvm/llvm-project/commit/97e0e67e311cbb4ddc68f14d20c585fed3769533.diff

LOG: [clang][C23] Delay thread storage recovery for constexpr diagnostics (#236758)

When a thread storage specifier conflicts with another storage class,
DeclSpec clears it before diagnosing a C23 constexpr conflict. The
second diagnostic then reports "unspecified" and loses the specifier's
source range.

For a block-scope declaration:

Before:
```
a:2:22: error: cannot combine with previous 'register' declaration specifier
    2 |   constexpr register _Thread_local int x = 1;
      |             ~~~~~~~~ ^
a:2:3: error: cannot combine with previous 'unspecified' declaration specifier
    2 |   constexpr register _Thread_local int x = 1;
      |   ^
2 errors generated.
```

After:
```
a:2:22: error: cannot combine with previous 'register' declaration specifier
    2 |   constexpr register _Thread_local int x = 1;
      |             ~~~~~~~~ ^
a:2:3: error: cannot combine with previous '_Thread_local' declaration specifier
    2 |   constexpr register _Thread_local int x = 1;
      |   ^                  ~~~~~~~~~~~~~
2 errors generated.
```

Delay clearing the thread storage specifier until after the constexpr
diagnostic.

Added: 
    

Modified: 
    clang/lib/Sema/DeclSpec.cpp
    clang/test/Sema/constexpr.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/DeclSpec.cpp b/clang/lib/Sema/DeclSpec.cpp
index dfa7a79d1f92b..c4ff7fbc60821 100644
--- a/clang/lib/Sema/DeclSpec.cpp
+++ b/clang/lib/Sema/DeclSpec.cpp
@@ -1380,6 +1380,13 @@ void DeclSpec::CheckTypeSpec(Sema &S, const PrintingPolicy &Policy) {
   // _Thread_local can only appear with the 'static' and 'extern' storage class
   // specifiers. We also allow __private_extern__ as an extension.
   if (ThreadStorageClassSpec != TSCS_unspecified) {
+    if (S.getLangOpts().C23 &&
+        getConstexprSpecifier() == ConstexprSpecKind::Constexpr) {
+      S.Diag(ConstexprLoc, diag::err_invalid_decl_spec_combination)
+          << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
+          << SourceRange(getThreadStorageClassSpecLoc());
+    }
+
     switch (StorageClassSpec) {
     case SCS_unspecified:
     case SCS_extern:
@@ -1402,12 +1409,6 @@ void DeclSpec::CheckTypeSpec(Sema &S, const PrintingPolicy &Policy) {
       ThreadStorageClassSpec = TSCS_unspecified;
       ThreadStorageClassSpecLoc = SourceLocation();
     }
-    if (S.getLangOpts().C23 &&
-        getConstexprSpecifier() == ConstexprSpecKind::Constexpr) {
-      S.Diag(ConstexprLoc, diag::err_invalid_decl_spec_combination)
-          << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
-          << SourceRange(getThreadStorageClassSpecLoc());
-    }
   }
 
   if (S.getLangOpts().C23 && getTypeSpecType() != DeclSpec::TST_unspecified &&

diff  --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index 3bb8cffe16ab2..aedaba2cb921a 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -52,6 +52,11 @@ constexpr typedef int Bar; // expected-error {{typedef cannot be constexpr}}
 void f3(constexpr register int P1) { // expected-error {{function parameter cannot be constexpr}}
   constexpr register int V9 = 0;
   constexpr register auto V10 = 0.0;
+  // FIXME: The constexpr diagnostic calls a later _Thread_local "previous".
+  // Prefer "'_Thread_local' used with 'constexpr'".
+  constexpr register _Thread_local int V10a = 0;
+  // expected-error at -1 {{previous 'register' declaration specifier}}
+  // expected-error at -2 {{previous '_Thread_local' declaration specifier}}
 }
 
 constexpr thread_local int V11 = 38; // expected-error {{cannot combine with previous '_Thread_local' declaration specifier}}


        


More information about the cfe-commits mailing list