[clang] 26db3c3 - [clang][Interp] Handle discarding ConstantExprs

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 6 05:32:32 PST 2024


Author: Timm Bäder
Date: 2024-02-06T14:32:21+01:00
New Revision: 26db3c3b72d3c915ad296a5a5313210bde8ce3e1

URL: https://github.com/llvm/llvm-project/commit/26db3c3b72d3c915ad296a5a5313210bde8ce3e1
DIFF: https://github.com/llvm/llvm-project/commit/26db3c3b72d3c915ad296a5a5313210bde8ce3e1.diff

LOG: [clang][Interp] Handle discarding ConstantExprs

Assume no side-effects in the presence of a cashed result in the form
of an APValue. This is also what the current interpreter does.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/cxx20.cpp
    clang/test/SemaCXX/cxx11-default-member-initializers.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 10e32d9b7bcf3..f31755e72e8de 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1030,14 +1030,17 @@ bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr(
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
-  // Try to emit the APValue directly, without visiting the subexpr.
-  // This will only fail if we can't emit the APValue, so won't emit any
-  // diagnostics or any double values.
   std::optional<PrimType> T = classify(E->getType());
-  if (T && E->hasAPValueResult() &&
-      this->visitAPValue(E->getAPValueResult(), *T, E))
-    return true;
+  if (T && E->hasAPValueResult()) {
+    // Try to emit the APValue directly, without visiting the subexpr.
+    // This will only fail if we can't emit the APValue, so won't emit any
+    // diagnostics or any double values.
+    if (DiscardResult)
+      return true;
 
+    if (this->visitAPValue(E->getAPValueResult(), *T, E))
+      return true;
+  }
   return this->delegate(E->getSubExpr());
 }
 

diff  --git a/clang/test/AST/Interp/cxx20.cpp b/clang/test/AST/Interp/cxx20.cpp
index 50a7c02925878..0af553a77892e 100644
--- a/clang/test/AST/Interp/cxx20.cpp
+++ b/clang/test/AST/Interp/cxx20.cpp
@@ -752,3 +752,16 @@ namespace TryCatch {
   }
   static_assert(foo() == 11);
 }
+
+namespace IgnoredConstantExpr {
+  consteval int immediate() { return 0;}
+  struct ReferenceToNestedMembers {
+    int m;
+    int a = ((void)immediate(), m);
+    int b = ((void)immediate(), this->m);
+  };
+  struct ReferenceToNestedMembersTest {
+    void* m = nullptr;
+    ReferenceToNestedMembers j{0};
+  } test_reference_to_nested_members;
+}

diff  --git a/clang/test/SemaCXX/cxx11-default-member-initializers.cpp b/clang/test/SemaCXX/cxx11-default-member-initializers.cpp
index 9c18c73be8f68..dd8e9c6b7fc11 100644
--- a/clang/test/SemaCXX/cxx11-default-member-initializers.cpp
+++ b/clang/test/SemaCXX/cxx11-default-member-initializers.cpp
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -std=c++11 -verify %s -pedantic
+// RUN: %clang_cc1 -std=c++11 -verify %s -pedantic -fexperimental-new-constant-interpreter
 // RUN: %clang_cc1 -std=c++20 -verify %s -pedantic
+// RUN: %clang_cc1 -std=c++20 -verify %s -pedantic -fexperimental-new-constant-interpreter
 
 
 namespace PR31692 {


        


More information about the cfe-commits mailing list