[clang] [clang][Interp] Handle CXXTryStmts (PR #70584)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 28 23:46:36 PDT 2023


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

Just do the same thing the current interpreter does: Ignore all handlerrs and visit the try block like normal.

>From 0c0172a334a4484b4c1fa0d784fa7042e2fe805d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 29 Oct 2023 07:44:55 +0100
Subject: [PATCH] [clang][Interp] Handle CXXTryStmts

Just do the same thing the current interpreter does: Ignore all
handlerrs and visit the try block like normal.
---
 clang/lib/AST/Interp/ByteCodeStmtGen.cpp |  8 ++++++++
 clang/lib/AST/Interp/ByteCodeStmtGen.h   |  1 +
 clang/test/AST/Interp/cxx20.cpp          | 13 +++++++++++++
 3 files changed, 22 insertions(+)

diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index 509abe3ae867f93..b1ab5fcf9cb64c3 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -255,6 +255,8 @@ bool ByteCodeStmtGen<Emitter>::visitStmt(const Stmt *S) {
     return visitAsmStmt(cast<AsmStmt>(S));
   case Stmt::AttributedStmtClass:
     return visitAttributedStmt(cast<AttributedStmt>(S));
+  case Stmt::CXXTryStmtClass:
+    return visitCXXTryStmt(cast<CXXTryStmt>(S));
   case Stmt::NullStmtClass:
     return true;
   default: {
@@ -643,6 +645,12 @@ bool ByteCodeStmtGen<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
   return this->visitStmt(S->getSubStmt());
 }
 
+template <class Emitter>
+bool ByteCodeStmtGen<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
+  // Ignore all handlers.
+  return this->visitStmt(S->getTryBlock());
+}
+
 namespace clang {
 namespace interp {
 
diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.h b/clang/lib/AST/Interp/ByteCodeStmtGen.h
index 31f9dbb8064c73c..64e03587ab2112d 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.h
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.h
@@ -65,6 +65,7 @@ class ByteCodeStmtGen final : public ByteCodeExprGen<Emitter> {
   bool visitDefaultStmt(const DefaultStmt *S);
   bool visitAsmStmt(const AsmStmt *S);
   bool visitAttributedStmt(const AttributedStmt *S);
+  bool visitCXXTryStmt(const CXXTryStmt *S);
 
   bool emitLambdaStaticInvokerBody(const CXXMethodDecl *MD);
 
diff --git a/clang/test/AST/Interp/cxx20.cpp b/clang/test/AST/Interp/cxx20.cpp
index f0bb4e9e0d0711b..50a7c02925878d5 100644
--- a/clang/test/AST/Interp/cxx20.cpp
+++ b/clang/test/AST/Interp/cxx20.cpp
@@ -739,3 +739,16 @@ namespace NonPrimitiveOpaqueValue
 
   static_assert(!ternary(), "");
 }
+
+namespace TryCatch {
+  constexpr int foo() {
+    int a = 10;
+    try {
+      ++a;
+    } catch(int m) {
+      --a;
+    }
+    return a;
+  }
+  static_assert(foo() == 11);
+}



More information about the cfe-commits mailing list