[clang] [clang][bytecode] Support compile-time exceptions (PR #189410)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 31 05:15:47 PDT 2026
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/189410
>From 1596f128bef75f88848898a8a01d77fe2c5cec5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 10 Feb 2026 16:06:17 +0100
Subject: [PATCH] Exceptions
---
clang/include/clang/Basic/Builtins.td | 6 +
.../include/clang/Basic/DiagnosticASTKinds.td | 2 +-
clang/include/clang/Basic/UnsignedOrNone.h | 6 +
clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 3 +-
clang/lib/AST/ByteCode/ByteCodeEmitter.h | 10 +
clang/lib/AST/ByteCode/Compiler.cpp | 118 ++++++-
clang/lib/AST/ByteCode/Disasm.cpp | 2 +
clang/lib/AST/ByteCode/EvalEmitter.h | 11 +
clang/lib/AST/ByteCode/Function.h | 25 +-
clang/lib/AST/ByteCode/Interp.cpp | 111 ++++++-
clang/lib/AST/ByteCode/Interp.h | 146 ++++++++-
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 11 +
clang/lib/AST/ByteCode/InterpState.h | 14 +
clang/lib/AST/ByteCode/Opcodes.td | 27 +-
clang/lib/AST/ByteCode/PrimType.h | 9 +
clang/lib/AST/ByteCode/Source.h | 2 +
clang/test/AST/ByteCode/cxx20.cpp | 15 +-
clang/test/AST/ByteCode/cxx23.cpp | 4 +-
clang/test/AST/ByteCode/exceptions.cpp | 293 ++++++++++++++++++
clang/test/AST/ByteCode/invalid.cpp | 30 +-
clang/utils/TableGen/ClangOpcodesEmitter.cpp | 12 +-
21 files changed, 784 insertions(+), 73 deletions(-)
create mode 100644 clang/test/AST/ByteCode/exceptions.cpp
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index f1743c7286def..e95e872e70f07 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5554,3 +5554,9 @@ def CountedByRef : Builtin {
let Attributes = [NoThrow, CustomTypeChecking];
let Prototype = "int(...)";
}
+
+def ConstexprCurrentException: LangBuiltin<"CXX_LANG"> {
+ let Spellings = ["__builtin_current_exception"];
+ let Attributes = [NoThrow, Constexpr];
+ let Prototype = "void*()";
+}
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td b/clang/include/clang/Basic/DiagnosticASTKinds.td
index bde418695f647..b0def3ae404a1 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -406,7 +406,7 @@ def note_constexpr_infer_alloc_token_no_metadata : Note<
"could not get token metadata for inferred type">;
def note_constexpr_infer_alloc_token_stateful_mode
: Note<"stateful alloc token mode not supported in constexpr">;
-
+def note_constexpr_uncaught_exception : Note<"uncaught exception of type %0: '%1'">;
def warn_attribute_needs_aggregate : Warning<
"%0 attribute is ignored in non-aggregate type %1">,
InGroup<IgnoredAttributes>;
diff --git a/clang/include/clang/Basic/UnsignedOrNone.h b/clang/include/clang/Basic/UnsignedOrNone.h
index 659fd8c6487d2..aa09ae998dee0 100644
--- a/clang/include/clang/Basic/UnsignedOrNone.h
+++ b/clang/include/clang/Basic/UnsignedOrNone.h
@@ -35,6 +35,12 @@ struct UnsignedOrNone {
return Rep - 1;
}
+ unsigned value_or(unsigned U) const {
+ if (operator bool())
+ return operator*();
+ return U;
+ }
+
friend constexpr bool operator==(UnsignedOrNone LHS, UnsignedOrNone RHS) {
return LHS.Rep == RHS.Rep;
}
diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 393b8481fecd1..17cf076c52851 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -85,7 +85,8 @@ void ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl,
// Set the function's code.
Func->setCode(FuncDecl, NextLocalOffset, std::move(Code), std::move(SrcMap),
- std::move(Scopes), FuncDecl->hasBody(), IsValid);
+ std::move(Scopes), std::move(ExceptionTable),
+ FuncDecl->hasBody(), IsValid);
Func->setIsFullyCompiled(true);
}
diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.h b/clang/lib/AST/ByteCode/ByteCodeEmitter.h
index 102ce939c6717..97a3b141cae71 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.h
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.h
@@ -76,6 +76,14 @@ class ByteCodeEmitter {
llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
std::optional<SourceInfo> LocOverride = std::nullopt;
+ unsigned currentCodeSize() const { return Code.size(); }
+
+ void registerExceptionHandler(unsigned From, unsigned To, unsigned Target,
+ UnsignedOrNone DeclOffset, const Type *T) {
+ ExceptionTable.push_back(
+ ExceptionTableEntry{From, To, Target, DeclOffset, T});
+ }
+
private:
/// Current compilation context.
Context &Ctx;
@@ -91,9 +99,11 @@ class ByteCodeEmitter {
llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
/// Program code.
llvm::SmallVector<std::byte> Code;
+ llvm::SmallVector<ExceptionTableEntry> ExceptionTable;
/// Opcode to expression mapping.
SourceMap SrcMap;
+public:
/// Returns the offset for a jump or records a relocation.
int32_t getOffset(LabelTy Label);
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index c7f074c9efc6a..dfa568e172160 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -36,6 +36,21 @@ static std::optional<bool> getBoolValue(const Expr *E) {
return std::nullopt;
}
+// FIXME: Use this again.
+#if 0
+static bool blockEndsInReturn(const Stmt *S) {
+ if (isa<ReturnStmt>(S))
+ return true;
+
+ if (const auto *CS = dyn_cast<CompoundStmt>(S);
+ CS && !CS->body_empty()) {
+ return isa<ReturnStmt>(CS->body_back());
+ }
+
+ return false;
+}
+#endif
+
/// Scope used to handle temporaries in toplevel variable declarations.
template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
public:
@@ -3455,10 +3470,99 @@ bool Compiler<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) {
template <class Emitter>
bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
- if (E->getSubExpr() && !this->discard(E->getSubExpr()))
+ if (!Ctx.getLangOpts().CPlusPlus26 || !Ctx.getLangOpts().CXXExceptions) {
+ if (E->getSubExpr() && !this->discard(E->getSubExpr()))
+ return false;
+ return this->emitInvalid(E);
+ }
+
+ assert(E->getSubExpr()); // XXX
+
+ if (!this->visit(E->getSubExpr()))
+ return false;
+
+ // this->emitCleanup();
+
+ PrimType T = classify(E->getSubExpr()).value_or(PT_Ptr);
+ return this->emitThrow(T, E->getSubExpr()->getType().getTypePtr(), E);
+}
+
+template <class Emitter>
+bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
+ if (!Ctx.getLangOpts().CPlusPlus26 || !Ctx.getLangOpts().CXXExceptions) {
+ // Ignore all handlers.
+ return this->visitStmt(S->getTryBlock());
+ }
+
+ S->dumpColor();
+ unsigned NumHandlers = S->getNumHandlers();
+ llvm::errs() << "Handlers: " << NumHandlers << '\n';
+
+ unsigned s = this->currentCodeSize();
+
+ // Emit try block contents.
+ const auto *TryBlock = cast<CompoundStmt>(S->getTryBlock());
+ LocalScope<Emitter> TryBlockScope(this);
+ for (const auto *InnerStmt : TryBlock->body()) {
+ if (!visitStmt(InnerStmt))
+ return false;
+ }
+
+ if (!TryBlockScope.destroyLocals())
return false;
+ // Unlink this scope. We will emit cleanups for it again later.
+ this->VarScope = TryBlockScope.getParent();
+
+ unsigned e = this->currentCodeSize();
+
+ // Jump after handlers if nothing was thrown.
+ LabelTy EndLabel = this->getLabel();
+ this->jump(EndLabel, S);
+
+ // Register and emit all handlers.
+ for (unsigned I = 0; I != NumHandlers; ++I) {
+ const CXXCatchStmt *Handler = S->getHandler(I);
+ const Stmt *Block = Handler->getHandlerBlock();
+ const VarDecl *ExceptionDecl = Handler->getExceptionDecl();
+ QualType CatchType = Handler->getCaughtType();
+ UnsignedOrNone ExceptionDeclOffset = std::nullopt;
+
+ unsigned t = this->currentCodeSize();
+ if (ExceptionDecl) {
+ PrimType T = classify(CatchType).value_or(PT_Ptr);
+ // FIXME: Scopes?
+ if (!this->visitDecl(ExceptionDecl))
+ return false;
+ ExceptionDeclOffset = Locals.find(ExceptionDecl)->second.Offset;
+
+ if (!this->emitGetExceptionValue(T, S))
+ return false;
+ if (!this->emitSetLocal(T, Locals.find(ExceptionDecl)->second.Offset, S))
+ return false;
+ } else {
+ // This is a catch-all handler.
+ if (!this->emitClearExceptionValue(S))
+ return false;
+ }
+
+ const Type *CatchTypePtr = CatchType.getTypePtrOrNull();
- return this->emitInvalid(E);
+ this->registerExceptionHandler(s, e, t, ExceptionDeclOffset, CatchTypePtr);
+ if (!this->visitStmt(Block))
+ return false;
+ // FIXME: Re-enable this.
+ // if (blockEndsInReturn(Block))
+ // continue;
+ this->jump(EndLabel, S);
+ }
+
+ this->fallthrough(EndLabel);
+ this->emitLabel(EndLabel);
+
+ if (!TryBlockScope.destroyLocals())
+ return false;
+
+ return true;
}
template <class Emitter>
@@ -6584,12 +6688,6 @@ bool Compiler<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
return true;
}
-template <class Emitter>
-bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
- // Ignore all handlers.
- return this->visitStmt(S->getTryBlock());
-}
-
template <class Emitter>
bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
assert(MD->isLambdaStaticInvoker());
@@ -6949,8 +7047,8 @@ bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) {
// Emit a guard return to protect against a code path missing one.
if (F->getReturnType()->isVoidType())
- return this->emitRetVoid(SourceInfo{});
- return this->emitNoRet(SourceInfo{});
+ return this->emitRetVoid(SourceInfo{}) && this->emitAfterRet(SourceInfo{});
+ return this->emitNoRet(SourceInfo{}) && this->emitAfterRet(SourceInfo{});
}
static uint32_t getBitWidth(const Expr *E) {
diff --git a/clang/lib/AST/ByteCode/Disasm.cpp b/clang/lib/AST/ByteCode/Disasm.cpp
index 6caa33261dad6..a5c61178bffd4 100644
--- a/clang/lib/AST/ByteCode/Disasm.cpp
+++ b/clang/lib/AST/ByteCode/Disasm.cpp
@@ -179,6 +179,8 @@ LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS,
Text.Addr = Addr;
Text.IsJump = isJumpOpcode(Op);
Text.CurrentOp = (PC == OpPC);
+ // llvm::errs() << (void*)(*PC) << " / " << (void*)(*OpPC) << ((*OpPC -
+ // *PC)) << '\n';
switch (Op) {
#define GET_DISASM
#include "Opcodes.inc"
diff --git a/clang/lib/AST/ByteCode/EvalEmitter.h b/clang/lib/AST/ByteCode/EvalEmitter.h
index 8f6da7aef422a..479c659d7a120 100644
--- a/clang/lib/AST/ByteCode/EvalEmitter.h
+++ b/clang/lib/AST/ByteCode/EvalEmitter.h
@@ -54,6 +54,17 @@ class EvalEmitter : public SourceMapper {
virtual ~EvalEmitter();
+ unsigned getOffset(LabelTy L) { return 0; }
+ unsigned currentCodeSize() const {
+ llvm_unreachable("Should never be called on EvalEmitter");
+ return 0;
+ }
+
+ void registerExceptionHandler(unsigned From, unsigned To, unsigned Target,
+ UnsignedOrNone, const Type *T) {
+ llvm_unreachable("Should never be called on EvalEmitter");
+ }
+
/// Define a label.
void emitLabel(LabelTy Label);
/// Create a label.
diff --git a/clang/lib/AST/ByteCode/Function.h b/clang/lib/AST/ByteCode/Function.h
index 90732d6dc01a4..8f3b11c215916 100644
--- a/clang/lib/AST/ByteCode/Function.h
+++ b/clang/lib/AST/ByteCode/Function.h
@@ -63,6 +63,15 @@ class Scope final {
LocalVectorTy Descriptors;
};
+struct ExceptionTableEntry {
+ unsigned CodeStart;
+ unsigned CodeEnd;
+ unsigned Target;
+ UnsignedOrNone DeclOffset;
+ /// If CatchType is nullptr, this is a catch-all handler.
+ const Type *CatchType;
+};
+
using FunctionDeclTy =
llvm::PointerUnion<const FunctionDecl *, const BlockExpr *>;
@@ -247,6 +256,16 @@ class Function final {
return false;
}
+ unsigned getParamOffset(unsigned ParamIndex) const {
+ return ParamDescriptors[ParamIndex].Offset;
+ }
+
+ PrimType getParamType(unsigned ParamIndex) const {
+ return ParamDescriptors[ParamIndex].T;
+ }
+
+ llvm::SmallVector<ExceptionTableEntry> ExceptionTable;
+
private:
/// Construct a function representing an actual function.
Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
@@ -256,13 +275,15 @@ class Function final {
/// Sets the code of a function.
void setCode(FunctionDeclTy Source, unsigned NewFrameSize,
llvm::SmallVector<std::byte> &&NewCode, SourceMap &&NewSrcMap,
- llvm::SmallVector<Scope, 2> &&NewScopes, bool NewHasBody,
- bool NewIsValid) {
+ llvm::SmallVector<Scope, 2> &&NewScopes,
+ llvm::SmallVector<ExceptionTableEntry> &&ExceptionTable,
+ bool NewHasBody, bool NewIsValid) {
this->Source = Source;
FrameSize = NewFrameSize;
Code = std::move(NewCode);
SrcMap = std::move(NewSrcMap);
Scopes = std::move(NewScopes);
+ this->ExceptionTable = std::move(ExceptionTable);
IsValid = NewIsValid;
HasBody = NewHasBody;
}
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 0a13456e6026d..2102bfc1d1bac 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -24,6 +24,7 @@
#include "clang/Basic/DiagnosticSema.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/StringExtras.h"
+#include <variant>
using namespace clang;
using namespace clang::interp;
@@ -752,7 +753,7 @@ bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
// Similarly, for local loads.
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
assert(!B->isExtern());
- const auto &Desc = *reinterpret_cast<const InlineDescriptor *>(B->rawData());
+ const auto &Desc = B->getBlockDesc<const InlineDescriptor>();
if (!CheckLifetime(S, OpPC, Desc.LifeState, AK_Read))
return false;
if (!Desc.IsInitialized)
@@ -1228,7 +1229,7 @@ static bool runRecordDestructor(InterpState &S, CodePtr OpPC,
return false;
S.Stk.push<Pointer>(BasePtr);
- return Call(S, OpPC, DtorFunc, 0);
+ return Call(S, OpPC, OpPC, DtorFunc, 0);
}
static bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) {
@@ -1634,16 +1635,16 @@ bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
S.Current = FrameBefore;
return false;
}
-bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
+bool Call(InterpState &S, CodePtr &PC, CodePtr OpPC, const Function *Func,
uint32_t VarArgSize) {
-
+ // CodePtr OpPC = PC - align(sizeof(uint32_t)) - align(sizeof(void*));
// C doesn't have constexpr functions.
if (!S.getLangOpts().CPlusPlus)
- return Invalid(S, OpPC);
+ return Invalid(S, PC);
assert(Func);
auto cleanup = [&]() -> bool {
- cleanupAfterFunctionCall(S, OpPC, Func);
+ cleanupAfterFunctionCall(S, PC, Func);
return false;
};
@@ -1701,6 +1702,8 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
auto Memory = new char[InterpFrame::allocSize(Func)];
auto NewFrame = new (Memory) InterpFrame(S, Func, OpPC, VarArgSize);
+ Func->dump();
+
InterpFrame *FrameBefore = S.Current;
S.Current = NewFrame;
@@ -1709,6 +1712,9 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
// Ret() above only sets the APValue if the curent frame doesn't
// have a caller set.
bool Success = Interpret(S);
+
+ llvm::errs() << ">> Call end\n";
+
// Remove initializing block again.
if (Func->isConstructor() || Func->isDestructor())
S.InitializingBlocks.pop_back();
@@ -1721,7 +1727,72 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
return false;
}
- assert(S.Current == FrameBefore);
+ llvm::errs() << "Frame after call: " << S.Current->getName() << '\n';
+ ;
+
+ if (S.ThrownValue) {
+ llvm::errs() << "WE HAVE A THROWN VALUE in CALL\n";
+ // If we have a thrown value in the InterpState, it was thrown in the
+ // function we just called (or deeper down in the stack), but not caught. We
+ // now need to check if the current function can call it.
+ const Function *CurrFunction = S.Current->getFunction();
+ if (!CurrFunction) {
+ assert(S.Current->isBottomFrame());
+ if (!S.checkingPotentialConstantExpression()) {
+ QualType UncaughtType = QualType(S.ThrownValue->Ty, 0);
+ std::string ValString;
+ TYPE_SWITCH(S.ThrownValue->T, {
+ ValString = std::get<T>(S.ThrownValue->Value)
+ .toDiagnosticString(S.getASTContext());
+ });
+ S.FFDiag(S.ThrownValue->ThrowSite,
+ diag::note_constexpr_uncaught_exception)
+ << UncaughtType
+ << ValString; // QualType(Ty, 0) <<
+ // "";//ThrownValue.toDiagnosticString(S.getASTContext());
+ }
+ return false;
+ }
+ auto canCatch = [](const Type *CatchType, const Type *ThrowType) -> bool {
+ if (!CatchType || ASTContext::hasSameType(CatchType, ThrowType))
+ return true;
+
+ assert(CatchType);
+
+ if (const auto *L = CatchType->getAs<ReferenceType>()) {
+ return L->getPointeeType().getTypePtr() == ThrowType;
+ }
+
+ // nullptr_t can be caught by any pointer type.
+ if (ThrowType->isNullPtrType() && CatchType->isPointerType())
+ return true;
+
+ // void* can catch all thown pointer types.
+ if (ThrowType->isPointerType() && CatchType->isVoidPointerType())
+ return true;
+
+ return false;
+ };
+
+ bool Caught = false;
+ unsigned CodeOffset = PC - CurrFunction->getCodeBegin();
+ for (const auto &E : CurrFunction->ExceptionTable) {
+ if (E.CodeStart <= CodeOffset && E.CodeEnd >= CodeOffset &&
+ (canCatch(E.CatchType, S.ThrownValue->Ty))) {
+ llvm::errs() << "AAAAAAAHA! IN " << CurrFunction->getName() << "\n";
+ PC = S.Current->getFunction()->getCodeBegin() + E.Target;
+
+ Caught = true;
+ break;
+ }
+ }
+
+ if (!Caught) {
+ PC = S.Current->getFunction()->getCodeEnd() - align(sizeof(Opcode));
+ }
+ }
+
+ // assert(S.Current == FrameBefore);
return true;
}
@@ -1815,7 +1886,7 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
}
}
- if (!Call(S, OpPC, Func, VarArgSize))
+ if (!Call(S, OpPC, OpPC, Func, VarArgSize))
return false;
// Covariant return types. The return type of Overrider is a pointer
@@ -1910,7 +1981,7 @@ bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
if (F->isVirtual())
return CallVirt(S, OpPC, F, VarArgSize);
- return Call(S, OpPC, F, VarArgSize);
+ return Call(S, OpPC, OpPC, F, VarArgSize);
}
static void startLifetimeRecurse(const Pointer &Ptr) {
@@ -2586,14 +2657,22 @@ bool Interpret(InterpState &S) {
return InterpNext(S, PC);
#else
while (true) {
- auto Op = PC.read<Opcode>();
- auto Fn = InterpFunctions[Op];
+ // Empty program.
+ if (!PC)
+ return true;
- if (!Fn(S, PC))
- return false;
- if (OpReturns(Op))
- break;
- }
+ for (;;) {
+ if (S.Current->getFunction())
+ llvm::errs() << "Interpret loop: "
+ << S.Current->getFunction()->getName() << '\n';
+ auto Op = PC.read<Opcode>();
+ auto Fn = InterpFunctions[Op];
+
+ if (!Fn(S, PC))
+ return false;
+ if (OpReturns(Op))
+ break;
+ }
return true;
#endif
}
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 3578ef9da820b..437742927c7c7 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -26,6 +26,7 @@
#include "InterpStack.h"
#include "InterpState.h"
#include "MemberPointer.h"
+#include "Opcode.h"
#include "PrimType.h"
#include "Program.h"
#include "State.h"
@@ -113,7 +114,7 @@ bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
uint32_t VarArgSize);
-bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
+bool Call(InterpState &S, CodePtr &PC, CodePtr OpPC, const Function *Func,
uint32_t VarArgSize);
bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
uint32_t VarArgSize);
@@ -246,12 +247,17 @@ template <PrimType Name, class T = typename PrimConv<Name>::T>
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC) {
const T &Ret = S.Stk.pop<T>();
+ llvm::errs() << __PRETTY_FUNCTION__ << ": " << Ret << '\n';
+
assert(S.Current);
assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
cleanupAfterFunctionCall(S, PC, S.Current->getFunction());
if (InterpFrame *Caller = S.Current->Caller) {
+ // if (S.Current->Caller->getFunction())
+ // llvm::errs() << "Ret: " << (S.Current->getRetPC() -
+ // S.Current->Caller->getFunction()->getCodeBegin()) << '\n';
PC = S.Current->getRetPC();
InterpFrame::free(S.Current);
S.Current = Caller;
@@ -268,6 +274,7 @@ PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC) {
PRESERVE_NONE inline bool RetVoid(InterpState &S, CodePtr &PC) {
assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
+ llvm::errs() << __PRETTY_FUNCTION__ << '\n';
if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
cleanupAfterFunctionCall(S, PC, S.Current->getFunction());
@@ -284,6 +291,108 @@ PRESERVE_NONE inline bool RetVoid(InterpState &S, CodePtr &PC) {
return true;
}
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool Throw(InterpState &S, CodePtr &PC, const Type *Ty) {
+ llvm::errs() << "############################################################"
+ "###################################\n";
+ CodePtr OpPC = PC - align(sizeof(Opcode));
+ // Can A catch B?
+ auto canCatch = [](const Type *CatchType, const Type *ThrowType) -> bool {
+ if (!CatchType || ASTContext::hasSameType(CatchType, ThrowType))
+ return true;
+
+ assert(CatchType);
+
+ if (const auto *L = CatchType->getAs<ReferenceType>()) {
+ return L->getPointeeType().getTypePtr() == ThrowType;
+ }
+
+ // nullptr_t can be caught by any pointer type.
+ if (ThrowType->isNullPtrType() && CatchType->isPointerType())
+ return true;
+
+ // void* can catch all thown pointer types.
+ if (ThrowType->isPointerType() && CatchType->isVoidPointerType())
+ return true;
+
+ return false;
+ };
+
+ const T &ThrownValue = S.Stk.pop<T>();
+
+ assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
+ llvm::errs() << "\n\n" << __PRETTY_FUNCTION__ << '\n';
+ llvm::errs() << "Throwing " << ThrownValue << '\n';
+ llvm::errs() << "In " << S.Current->getName() << '\n';
+ unsigned O = PC - S.Current->getFunction()->getCodeBegin();
+ llvm::errs() << "O: " << O << ". Type: " << Ty << '\n';
+ bool Caught = false;
+
+ llvm::errs() << "Thrown Type:\n";
+ Ty->dump();
+
+ const Function *F = S.Current->getFunction();
+ if (!F) {
+ assert(false);
+ }
+ CodePtr CurrentPC = PC;
+ llvm::errs() << "Current function: " << F->getName() << '\n';
+ unsigned CodeOffset = CurrentPC - F->getCodeBegin();
+ llvm::errs() << "CodeOffset: " << CodeOffset << '\n';
+
+ for (const auto &E : F->ExceptionTable) {
+ llvm::errs() << E.CodeStart << ".." << E.CodeEnd << " -> " << E.Target
+ << ". Type: " << E.CatchType
+ << ". DeclOffset: " << E.DeclOffset.value_or(-1) << '\n';
+
+ if (E.CatchType) {
+ llvm::errs() << "Catching:\n";
+ E.CatchType->dump();
+ }
+
+ if (E.CodeStart <= CodeOffset && E.CodeEnd >= CodeOffset &&
+ (canCatch(E.CatchType, Ty))) {
+ llvm::errs() << "SAME FRAME\n";
+ PC = S.Current->getFunction()->getCodeBegin() + E.Target;
+
+ llvm::errs() << "new offset: "
+ << (PC - S.Current->getFunction()->getCodeBegin()) << '\n';
+
+ llvm::errs() << "FOUND!\n";
+ // if (E.DeclOffset) {
+ // llvm::errs() << "Setting local to " << ThrownValue << "\n";
+ // S.Current->setLocal<T>(*E.DeclOffset, ThrownValue);
+ // llvm::errs() << "Value now: " << S.Current->getLocal<T>(*E.DeclOffset)
+ // << '\n';
+ // }
+ Caught = true;
+
+ // S.Current->getFunction()->dump();
+ break;
+ }
+ }
+
+ const CXXThrowExpr *Source = cast<CXXThrowExpr>(S.Current->getExpr(OpPC));
+ S.ThrownValue = ThrowValue{Ty, Source, ThrownValue, Name};
+
+ if (Caught) {
+ llvm::errs() << "CAUGHT IN " << F->getName() << '\n';
+ } else {
+ llvm::errs() << "Uncaught exception!\n";
+ // If we didnt' catch the exception in the current frame, save the thrown
+ // value in InterpState to be caught by callers of this function.
+ // const CXXThrowExpr *Source =
+ // cast<CXXThrowExpr>(S.Current->getExpr(OpPC)); S.ThrownValue =
+ // ThrowValue{Ty, Source, ThrownValue, Name};
+ PC = S.Current->getFunction()->getCodeEnd() - align(sizeof(Opcode));
+ llvm::errs() << "Offset now: "
+ << (PC - S.Current->getFunction()->getCodeBegin()) << '\n';
+ // PC = S.Current->getFunction()->getCodeEnd();// - align(sizeof(Opcode));
+ }
+
+ return true;
+}
+
//===----------------------------------------------------------------------===//
// Add, Sub, Mul
//===----------------------------------------------------------------------===//
@@ -2519,6 +2628,7 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC, bool ElemSizeIsZero) {
}
inline bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I) {
+ llvm::errs() << __PRETTY_FUNCTION__ << '\n';
S.Current->initScope(I);
return true;
}
@@ -3097,6 +3207,25 @@ PRESERVE_NONE inline bool NoRet(InterpState &S, CodePtr OpPC) {
S.FFDiag(EndLoc, diag::note_constexpr_no_return);
return false;
}
+PRESERVE_NONE inline bool AfterRet(InterpState &S, CodePtr &PC) {
+
+ assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
+ llvm::errs() << __PRETTY_FUNCTION__ << '\n';
+
+ if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
+ cleanupAfterFunctionCall(S, PC, S.Current->getFunction());
+
+ if (InterpFrame *Caller = S.Current->Caller) {
+ PC = S.Current->getRetPC();
+ InterpFrame::free(S.Current);
+ S.Current = Caller;
+ } else {
+ InterpFrame::free(S.Current);
+ S.Current = nullptr;
+ }
+
+ return true;
+}
//===----------------------------------------------------------------------===//
// NarrowPtr, ExpandPtr
@@ -3729,6 +3858,21 @@ inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
return CheckDestructor(S, OpPC, Ptr);
}
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool GetExceptionValue(InterpState &S, CodePtr OpPC) {
+ assert(S.ThrownValue);
+
+ S.Stk.push<T>(std::get<T>(S.ThrownValue->Value));
+ S.ThrownValue = std::nullopt;
+ return true;
+}
+
+inline bool ClearExceptionValue(InterpState &S, CodePtr OpPC) {
+ assert(S.ThrownValue);
+ S.ThrownValue = std::nullopt;
+ return true;
+}
+
//===----------------------------------------------------------------------===//
// Read opcode arguments
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index e7b3ef6ce1510..8a18482f5b5ff 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -4189,6 +4189,15 @@ static bool interp__builtin_ia32_gfni_mul(InterpState &S, CodePtr OpPC,
return true;
}
+static bool interp__builtin_current_exception(InterpState &S, CodePtr OpPC,
+ const CallExpr *Call) {
+ // llvm::errs() <<__PRETTY_FUNCTION__ << '\n';
+ // Call->dumpColor();
+
+ S.Stk.push<Pointer>();
+ return true;
+}
+
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
uint32_t BuiltinID) {
if (!S.getASTContext().BuiltinInfo.isConstantEvaluated(BuiltinID))
@@ -6050,6 +6059,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
},
/*IsScalar=*/true);
+ case Builtin::BI__builtin_current_exception:
+ return interp__builtin_current_exception(S, OpPC, Call);
default:
S.FFDiag(S.Current->getLocation(OpPC),
diag::note_invalid_subexpr_in_const_expr)
diff --git a/clang/lib/AST/ByteCode/InterpState.h b/clang/lib/AST/ByteCode/InterpState.h
index 499a21a094e2c..5c0aee4604a36 100644
--- a/clang/lib/AST/ByteCode/InterpState.h
+++ b/clang/lib/AST/ByteCode/InterpState.h
@@ -13,12 +13,17 @@
#ifndef LLVM_CLANG_AST_INTERP_INTERPSTATE_H
#define LLVM_CLANG_AST_INTERP_INTERPSTATE_H
+#include "Boolean.h"
#include "Context.h"
#include "DynamicAllocator.h"
#include "Floating.h"
#include "Function.h"
+#include "Integral.h"
+#include "IntegralAP.h"
#include "InterpFrame.h"
#include "InterpStack.h"
+#include "MemberPointer.h"
+#include "Pointer.h"
#include "State.h"
namespace clang {
@@ -32,6 +37,13 @@ struct StdAllocatorCaller {
explicit operator bool() { return Call; }
};
+struct ThrowValue {
+ const Type *Ty;
+ const Expr *ThrowSite;
+ AnyPrimType Value;
+ PrimType T;
+};
+
/// Interpreter context.
class InterpState final : public State, public SourceMapper {
public:
@@ -162,6 +174,8 @@ class InterpState final : public State, public SourceMapper {
/// ID identifying this evaluation.
const unsigned EvalID;
+ std::optional<ThrowValue> ThrownValue = std::nullopt;
+
/// Things needed to do speculative execution.
SmallVectorImpl<PartialDiagnosticAt> *PrevDiags = nullptr;
#ifndef NDEBUG
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 5e4d0ab2a84af..3cbe6288b8b65 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -142,6 +142,7 @@ class Opcode {
string Name = "";
bit CanReturn = 0;
bit ChangesPC = 0;
+ bit BothPCs = 0;
bit HasCustomLink = 0;
bit HasCustomEval = 0;
bit HasGroup = 0;
@@ -217,10 +218,34 @@ def RetValue : Opcode {
def NoRet : Opcode {}
+/// Exceptions
+def AfterRet : Opcode {
+ let CanReturn = 1;
+}
+
+def Throw : Opcode {
+ let Types = [AllTypeClass];
+ // let CanReturn = 1;
+ let ChangesPC = 1;
+ let HasGroup = 1;
+ let Args = [ArgTypePtr];
+ // let HasCustomEval = 1;
+}
+
+def GetExceptionValue : Opcode {
+ let Types = [AllTypeClass];
+ let HasGroup = 1;
+}
+
+def ClearExceptionValue : Opcode;
+
+
+
def Call : Opcode {
let Args = [ArgFunction, ArgUint32];
+ let ChangesPC = 1;
+ let BothPCs = 1;
}
-
def CallVirt : Opcode {
let Args = [ArgFunction, ArgUint32];
}
diff --git a/clang/lib/AST/ByteCode/PrimType.h b/clang/lib/AST/ByteCode/PrimType.h
index 2fa553b7b4a47..e6a65c9297fa1 100644
--- a/clang/lib/AST/ByteCode/PrimType.h
+++ b/clang/lib/AST/ByteCode/PrimType.h
@@ -17,6 +17,7 @@
#include <climits>
#include <cstddef>
#include <cstdint>
+#include <variant>
namespace clang {
namespace interp {
@@ -48,6 +49,14 @@ enum PrimType : uint8_t {
PT_MemberPtr = 14,
};
+// Alias for using any one of our primitive types.
+using AnyPrimType =
+ std::variant<Integral<8, true>, Integral<8, false>, Integral<16, true>,
+ Integral<16, false>, Integral<32, true>, Integral<32, false>,
+ Integral<64, true>, Integral<64, false>, IntegralAP<true>,
+ IntegralAP<false>, Boolean, FixedPoint, Floating, Pointer,
+ MemberPointer>;
+
constexpr bool isIntegerOrBoolType(PrimType T) { return T <= PT_Bool; }
constexpr bool isIntegerType(PrimType T) { return T <= PT_IntAPS; }
diff --git a/clang/lib/AST/ByteCode/Source.h b/clang/lib/AST/ByteCode/Source.h
index 56ca197e66473..464c1c8bc9811 100644
--- a/clang/lib/AST/ByteCode/Source.h
+++ b/clang/lib/AST/ByteCode/Source.h
@@ -36,6 +36,8 @@ class CodePtr final {
return *this;
}
+ CodePtr operator+(int32_t Offset) { return CodePtr(Ptr + Offset); }
+
int32_t operator-(const CodePtr &RHS) const {
assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
return Ptr - RHS.Ptr;
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index 9800fe01fcaf5..300dc0f02591e 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,expected -fcxx-exceptions %s -DNEW_INTERP -fexperimental-new-constant-interpreter
-// RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,ref -fcxx-exceptions %s
+// RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,expected %s -DNEW_INTERP -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,ref %s
void test_alignas_operand() {
alignas(8) char dummy;
@@ -654,7 +654,7 @@ namespace ConstexprArrayInitLoopExprDestructors
struct Highlander {
int *p = 0;
constexpr Highlander() {}
- constexpr void set(int *p) { this->p = p; ++*p; if (*p != 1) throw "there can be only one"; }
+ constexpr void set(int *p) { this->p = p; ++*p; if (*p != 1) __builtin_abort(); }
constexpr ~Highlander() { --*p; }
};
@@ -757,7 +757,7 @@ namespace FailingDestructor {
constexpr ~D() {
if (!can_destroy)
- throw "oh no";
+ __builtin_abort();
}
};
template<D d>
@@ -1043,7 +1043,7 @@ namespace OnePastEndDtor {
namespace Virtual {
struct NonZeroOffset { int padding = 123; };
- constexpr void assert(bool b) { if (!b) throw 0; }
+ constexpr void assert(bool b) { if (!b) __builtin_abort(); }
// Ensure that we pick the right final overrider during construction.
struct A {
@@ -1172,9 +1172,8 @@ namespace DiscardedTrivialCXXConstructExpr {
int x;
};
- constexpr int foo(int x) { // ref-error {{never produces a constant expression}}
- throw S(3); // both-note {{not valid in a constant expression}} \
- // ref-note {{not valid in a constant expression}}
+ constexpr int foo(int x) { // both-error {{never produces a constant expression}}
+ __builtin_abort(); // both-note 2{{not valid in a constant expression}}
return 1;
}
diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp
index a1aecf329327a..111b7f88eaa29 100644
--- a/clang/test/AST/ByteCode/cxx23.cpp
+++ b/clang/test/AST/ByteCode/cxx23.cpp
@@ -129,8 +129,8 @@ namespace StaticOperators {
struct S1 {
constexpr S1() { // all20-error {{never produces a constant expression}}
- throw; // all-note {{not valid in a constant expression}} \
- // all20-note {{not valid in a constant expression}}
+ __builtin_abort(); // all-note {{not valid in a constant expression}} \
+ // all20-note {{not valid in a constant expression}}
}
static constexpr int operator()() { return 3; } // ref20-warning {{C++23 extension}} \
// expected20-warning {{C++23 extension}}
diff --git a/clang/test/AST/ByteCode/exceptions.cpp b/clang/test/AST/ByteCode/exceptions.cpp
new file mode 100644
index 0000000000000..b1f1526aff124
--- /dev/null
+++ b/clang/test/AST/ByteCode/exceptions.cpp
@@ -0,0 +1,293 @@
+// RUN: %clang_cc1 -fcxx-exceptions -std=c++26 -fexperimental-new-constant-interpreter -verify %s
+
+namespace std {
+ class exception {
+ public:
+ constexpr exception() noexcept {};
+ // constexpr exception(const exception&) noexcept;
+ // constexpr exception& operator=(const exception&) noexcept;
+ constexpr virtual ~exception() {};
+ // constexpr virtual const char* what() const noexcept;
+ };
+};
+
+
+class Bad : std::exception {};
+
+namespace Simple {
+ constexpr int a() {
+ try {
+ } catch(int e){
+ return 12;
+ }
+ return -2;
+ }
+ static_assert(a() == -2);
+
+ constexpr int b() {
+ try {
+ throw 12;
+ } catch(int e){
+ return 12;
+ }
+ return -2;
+ }
+ static_assert(b() == 12);
+
+ constexpr int c() {
+ int m = 12;
+ try {
+ throw 12;
+ } catch(int e){
+ m = 140;
+ }
+ return m;
+ }
+ static_assert(c() == 140);
+
+ constexpr int d() {
+ int m = 12;
+ try {
+ throw 12;
+ } catch(int e){
+ m = 140;
+ } catch (float f) {
+ m = 15;
+ }
+ return m;
+ }
+ static_assert(d() == 140);
+
+ constexpr int e() {
+ int m = 12;
+ try {
+ throw 12;
+ } catch(int e){
+ m = e + 2;
+ }
+ return m;
+ }
+ static_assert(e() == 14);
+
+ constexpr int f() {
+ int m = 12;
+ try {
+ throw 12;
+ } catch(...){
+ m = 100;
+ }
+ return m;
+ }
+ static_assert(f() == 100);
+
+ constexpr int g() {
+ int m = 12;
+ try {
+ throw Bad();
+ } catch(Bad &B){
+ m = 100;
+ }
+ return m;
+ }
+ static_assert(g() == 100);
+
+ constexpr int h() {
+ int m = 12;
+ try {
+ throw ++m;
+ } catch(...){
+ }
+ return m;
+ }
+ static_assert(h() == 13);
+
+ constexpr int i(bool b) {
+ try {
+ if (b)
+ throw 12;
+ else
+ throw 14.0f;
+ } catch (int) {
+ return 100;
+ } catch (float) {
+ return 200;
+ }
+ return 0;
+ }
+ static_assert(i(true) == 100);
+ static_assert(i(false) == 200);
+}
+
+namespace Uncaught {
+
+ constexpr int a() {
+ throw 12; // expected-note {{uncaught exception of type 'int': '12'}}
+ return 0;
+ }
+ static_assert(a() == 13); // expected-error {{not an integral constant expression}}
+}
+
+namespace CleanupAfterThrowingCall {
+ constexpr int a() {
+ throw 12;
+ return -12;
+ }
+ constexpr int test() {
+ try {
+ a();
+ } catch (int i) {
+ return 26;
+ }
+
+ return 120;
+ }
+ static_assert(test() == 26);
+
+ constexpr int b2() {
+ throw 1.0;
+ return 1;
+ }
+ constexpr int a2() {
+ b2();
+ throw 12;
+ return -12;
+ }
+ constexpr int test2() {
+
+ try {
+ a2();
+ } catch (int i) {
+ return 26;
+ } catch (double d ){
+ return (int)(d * 2);
+ }
+
+ return 120;
+ }
+ static_assert(test2() == 2);
+}
+
+namespace Dtors {
+ class Inc {
+ public:
+ int &m;
+ constexpr Inc(int &m) : m(m) {}
+ constexpr ~Inc() { ++m; }
+ };
+
+ constexpr int test1() {
+ int m = 10;
+ Inc _(m);
+
+ try {
+ throw 12;
+ } catch (int) {
+ return m;
+ }
+ }
+ static_assert(test1() == 10);
+
+ constexpr int test2() {
+ int m = 10;
+ try {
+ Inc _(m);
+ throw 12;
+ } catch (int) {
+ }
+ return m;
+ }
+ static_assert(test2() == 11);
+
+}
+
+namespace CatchArray {
+ template <typename T> consteval T test(T head, auto... tail) {
+ const T array[] = {head, tail...};
+ try {
+ throw array;
+ } catch (const T (&arr)[5]) {
+ return -2;
+ } catch (const T * ptr) {
+ return *ptr;
+ } catch (...) {
+ return -1;
+ }
+ }
+
+ constexpr auto r0 = test(1,2,3,4,5,6);
+ static_assert(r0 == 1);
+
+ constexpr auto r1 = test(1,2,3,4,5);
+ static_assert(r1 == 1);
+
+ constexpr auto r2 = test(1,2,3,4);
+ static_assert(r2 == 1);
+
+ constexpr auto r3 = test(7,1,2,3,4,5,6);
+ static_assert(r3 == 7);
+
+ constexpr auto r4 = test(8,1,2,3,4,5);
+ static_assert(r4 == 8);
+
+ constexpr auto r5 = test(9,1,2,3,4);
+ static_assert(r5 == 9);
+}
+
+namespace CatchVoidPtr {
+ consteval int test() {
+ int p = 3;
+ try {
+ throw &p;
+ } catch (void * ptr) {
+ return *static_cast<int *>(ptr);
+ }
+ }
+
+ static_assert(test() == 3);
+}
+
+namespace Nullptr {
+ consteval int test_nullptr() {
+ try {
+ throw nullptr;
+ } catch (const int * ex) {
+ return true;
+ } catch (...) {
+ return false;
+ }
+ }
+ static_assert(test_nullptr());
+
+ consteval int test_zero() {
+ try {
+ throw 0;
+ } catch (const int * ex) {
+ return false;
+ } catch (...) {
+ return true;
+ }
+ }
+ static_assert(test_zero());
+}
+
+namespace CatchAll {
+ template <typename T, typename... Args> consteval int test(Args && ... args) {
+ try {
+ throw T{args...};
+ } catch (unsigned v) {
+ return static_cast<int>(v) * 2;
+ } catch (int v) {
+ return v * 3;
+ } catch (bool v) {
+ return static_cast<int>(v) * 5;
+ } catch (...) {
+ return -1;
+ }
+ return 0;
+ }
+
+ static_assert(test<unsigned>(42u) == 84);
+ static_assert(test<int>(13) == 39);
+ static_assert(test<bool>(true) == 5);
+ static_assert(test<long>(42) == -1);
+}
diff --git a/clang/test/AST/ByteCode/invalid.cpp b/clang/test/AST/ByteCode/invalid.cpp
index 9f157db889a22..207dce44c10b5 100644
--- a/clang/test/AST/ByteCode/invalid.cpp
+++ b/clang/test/AST/ByteCode/invalid.cpp
@@ -1,31 +1,5 @@
-// RUN: %clang_cc1 -triple x86_64 -fcxx-exceptions -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
-// RUN: %clang_cc1 -triple x86_64 -fcxx-exceptions -std=c++20 -verify=ref,both %s
-
-namespace Throw {
-
- constexpr int ConditionalThrow(bool t) {
- if (t)
- throw 4; // both-note {{subexpression not valid in a constant expression}}
-
- return 0;
- }
-
- static_assert(ConditionalThrow(false) == 0, "");
- static_assert(ConditionalThrow(true) == 0, ""); // both-error {{not an integral constant expression}} \
- // both-note {{in call to 'ConditionalThrow(true)'}}
-
- constexpr int Throw() { // both-error {{never produces a constant expression}}
- throw 5; // both-note {{subexpression not valid in a constant expression}}
- return 0;
- }
-
- constexpr int NoSubExpr() { // both-error {{never produces a constant expression}}
- throw; // both-note 2{{subexpression not valid}}
- return 0;
- }
- static_assert(NoSubExpr() == 0, ""); // both-error {{not an integral constant expression}} \
- // both-note {{in call to}}
-}
+// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
+// RUN: %clang_cc1 -triple x86_64 -std=c++20 -verify=ref,both %s
namespace Asm {
constexpr int ConditionalAsm(bool t) {
diff --git a/clang/utils/TableGen/ClangOpcodesEmitter.cpp b/clang/utils/TableGen/ClangOpcodesEmitter.cpp
index 3192891801ae9..504492feb9e55 100644
--- a/clang/utils/TableGen/ClangOpcodesEmitter.cpp
+++ b/clang/utils/TableGen/ClangOpcodesEmitter.cpp
@@ -127,6 +127,7 @@ void ClangOpcodesEmitter::EmitInterpFnDispatchers(raw_ostream &OS, StringRef N,
bool CanReturn = R->getValueAsBit("CanReturn");
const auto &Args = R->getValueAsListOfDefs("Args");
bool ChangesPC = R->getValueAsBit("ChangesPC");
+ bool BothPCs = R->getValueAsBit("BothPCs");
if (Args.empty()) {
if (CanReturn) {
@@ -152,7 +153,7 @@ void ClangOpcodesEmitter::EmitInterpFnDispatchers(raw_ostream &OS, StringRef N,
OS << " {\n";
- if (!ChangesPC)
+ if (!ChangesPC || BothPCs)
OS << " CodePtr OpPC = PC;\n";
// Emit calls to read arguments.
@@ -171,9 +172,11 @@ void ClangOpcodesEmitter::EmitInterpFnDispatchers(raw_ostream &OS, StringRef N,
OS << " if (!" << N;
PrintTypes(OS, TS);
OS << "(S";
- if (ChangesPC)
+ if (ChangesPC) {
OS << ", PC";
- else
+ if (BothPCs)
+ OS << ", OpPC";
+ } else
OS << ", OpPC";
for (size_t I = 0, N = Args.size(); I < N; ++I)
OS << ", V" << I;
@@ -407,6 +410,7 @@ void ClangOpcodesEmitter::EmitEval(raw_ostream &OS, StringRef N,
OS << (AsRef ? "const " : " ") << Name << " "
<< (AsRef ? "&" : "") << "A" << I << ", ";
}
+ bool BothPCs = R->getValueAsBit("BothPCs");
OS << "SourceInfo L) {\n";
OS << " if (!isActive()) return true;\n";
OS << " CurrentSource = L;\n";
@@ -414,6 +418,8 @@ void ClangOpcodesEmitter::EmitEval(raw_ostream &OS, StringRef N,
OS << " return " << N;
PrintTypes(OS, TS);
OS << "(S, OpPC";
+ if (BothPCs)
+ OS << ", OpPC";
for (size_t I = 0, N = Args.size(); I < N; ++I)
OS << ", A" << I;
OS << ");\n";
More information about the cfe-commits
mailing list