[clang] [clang][bytecode] Improve rejecting UnaryExprOrTypeTraitExprs (PR #180710)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 10 01:54:17 PST 2026


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

Some of them work just fine, even if the expression contains errors.

>From 665d6de9a632f5ff12e66389ab6afd0ba778f524 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 10 Feb 2026 10:53:09 +0100
Subject: [PATCH] [clang][bytecode] Improve rejecting UnaryExprOrTypeTraitExprs

Some of them work just fine, even if the expression contains errors.
---
 clang/lib/AST/ByteCode/Compiler.cpp             | 9 ++++++---
 clang/test/AST/ByteCode/invalid.cpp             | 5 +++++
 clang/test/SemaCXX/alignof-sizeof-reference.cpp | 1 +
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 526e1aee9ce3d..7aa7a8d75c8e1 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2256,9 +2256,6 @@ template <class Emitter>
 bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
     const UnaryExprOrTypeTraitExpr *E) {
 
-  if (E->containsErrors())
-    return false;
-
   UnaryExprOrTypeTrait Kind = E->getKind();
   const ASTContext &ASTCtx = Ctx.getASTContext();
 
@@ -2333,6 +2330,9 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
       // Argument is an expression, not a type.
       const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
 
+      if (Arg->getType()->isDependentType())
+        return false;
+
       // The kinds of expressions that we have special-case logic here for
       // should be kept up to date with the special checks for those
       // expressions in Sema.
@@ -2356,6 +2356,9 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
   }
 
   if (Kind == UETT_VectorElements) {
+    if (E->containsErrors())
+      return false;
+
     if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
       return this->emitConst(VT->getNumElements(), E);
     assert(E->getTypeOfArgument()->isSizelessVectorType());
diff --git a/clang/test/AST/ByteCode/invalid.cpp b/clang/test/AST/ByteCode/invalid.cpp
index 8c641a20a0bd4..20c37893c828b 100644
--- a/clang/test/AST/ByteCode/invalid.cpp
+++ b/clang/test/AST/ByteCode/invalid.cpp
@@ -170,3 +170,8 @@ constexpr int invalidUnaryOrTypeTrait() {
 }
 
 static_assert(invalidUnaryOrTypeTrait() == 11, ""); // both-error {{not an integral constant expression}}
+
+constexpr int invalidUnaryOrTypeTrait2() {
+  return alignof * 10; // both-error {{indirection requires pointer operand}} \
+                       // both-warning {{'alignof' applied to an expression is a GNU extension}}
+}
diff --git a/clang/test/SemaCXX/alignof-sizeof-reference.cpp b/clang/test/SemaCXX/alignof-sizeof-reference.cpp
index 3e37d615bbcf5..c5de165d38c77 100644
--- a/clang/test/SemaCXX/alignof-sizeof-reference.cpp
+++ b/clang/test/SemaCXX/alignof-sizeof-reference.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
 
 struct s0; // expected-note {{forward declaration}}
 char ar[sizeof(s0&)]; // expected-error {{invalid application of 'sizeof' to an incomplete type}}



More information about the cfe-commits mailing list