[clang] [clang][bytecode] Fix reporting non-constant variables in C (PR #109516)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 21 00:05:41 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/109516

We need to call FFDiag() to get the usual "invalid subexpression" diagnostic.

>From 568f6ecc3540fca57d2a7abaae99c9f07c737714 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 21 Sep 2024 09:02:49 +0200
Subject: [PATCH] [clang][bytecode] Fix reporting non-constant variables in C

We need to call FFDiag() to get the usual "invalid subexpression"
diagnostic.
---
 clang/lib/AST/ByteCode/Interp.cpp | 6 ++++--
 clang/test/Sema/annotate-type.c   | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 17cf3ccdeb6a94..d8e9fa838abaa8 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -90,10 +90,12 @@ static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
 
 static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
                                      const ValueDecl *VD) {
-  if (!S.getLangOpts().CPlusPlus)
+  const SourceInfo &Loc = S.Current->getSource(OpPC);
+  if (!S.getLangOpts().CPlusPlus) {
+    S.FFDiag(Loc);
     return;
+  }
 
-  const SourceInfo &Loc = S.Current->getSource(OpPC);
   if (const auto *VarD = dyn_cast<VarDecl>(VD);
       VarD && VarD->getType().isConstQualified() &&
       !VarD->getAnyInitializer()) {
diff --git a/clang/test/Sema/annotate-type.c b/clang/test/Sema/annotate-type.c
index 901cef7ffa8b3a..bf1632d302e554 100644
--- a/clang/test/Sema/annotate-type.c
+++ b/clang/test/Sema/annotate-type.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 %s -fsyntax-only -verify
+// RUN: %clang_cc1 %s -fsyntax-only -verify -fexperimental-new-constant-interpreter
 
 const char *some_function();
 



More information about the cfe-commits mailing list