[clang] [clang][bytecode] Diagnose non-const initialiers in diagnoseUnknownDecl (PR #113276)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 22 01:39:04 PDT 2024


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/113276

>From 093e6ee76698bf091eab46e8b73eb97d7a19544b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 22 Oct 2024 09:54:35 +0200
Subject: [PATCH] [clang][bytecode] Diagnose non-const initialiers in
 diagnoseUnknownDecl

This is more similar to the diagnostic output of the current interpreter
---
 clang/lib/AST/ByteCode/Interp.cpp                | 14 ++++++++++----
 clang/test/SemaCXX/c99-variable-length-array.cpp |  1 +
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index fdc4b38b8aa6dc..b7a6c224c80f8e 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -81,11 +81,17 @@ static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
     return false;
   }
 
-  if (!D->getType().isConstQualified())
+  if (!D->getType().isConstQualified()) {
     diagnoseNonConstVariable(S, OpPC, D);
-  else if (const auto *VD = dyn_cast<VarDecl>(D);
-           VD && !VD->getAnyInitializer())
-    diagnoseMissingInitializer(S, OpPC, VD);
+  } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
+    if (!VD->getAnyInitializer()) {
+      diagnoseMissingInitializer(S, OpPC, VD);
+    } else {
+      const SourceInfo &Loc = S.Current->getSource(OpPC);
+      S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
+      S.Note(VD->getLocation(), diag::note_declared_at);
+    }
+  }
 
   return false;
 }
diff --git a/clang/test/SemaCXX/c99-variable-length-array.cpp b/clang/test/SemaCXX/c99-variable-length-array.cpp
index 82ddb0fd2e2337..d9eb65e4355c31 100644
--- a/clang/test/SemaCXX/c99-variable-length-array.cpp
+++ b/clang/test/SemaCXX/c99-variable-length-array.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wvla-extension %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wvla-extension %s -fexperimental-new-constant-interpreter
 struct NonPOD {
   NonPOD();
 };



More information about the cfe-commits mailing list