[clang] [clang][bytecode] Fix rejecting invalid sizeof expressions (PR #123332)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 17 05:19:15 PST 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/123332
>From bcb3faf64f933ce4c9f1040af8eaec4d4957c09c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 17 Jan 2025 14:11:08 +0100
Subject: [PATCH] [clang][bytecode] Fix rejecting invalid sizeof expressions
Emit the invalid note instead of nothing.
---
clang/lib/AST/ByteCode/Compiler.cpp | 2 +-
clang/test/AST/ByteCode/literals.cpp | 25 ++++++++++---------------
2 files changed, 11 insertions(+), 16 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 4bfb80589620c1..32c6b6c29d693f 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2066,7 +2066,7 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
Size = CharUnits::One();
else {
if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
- return false;
+ return this->emitInvalid(E);
if (Kind == UETT_SizeOf)
Size = ASTCtx.getTypeSizeInChars(ArgType);
diff --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp
index 3d415a93a392a5..fdf1a6820e4466 100644
--- a/clang/test/AST/ByteCode/literals.cpp
+++ b/clang/test/AST/ByteCode/literals.cpp
@@ -261,31 +261,26 @@ namespace SizeOf {
}
#if __cplusplus >= 201402L
- constexpr int IgnoredRejected() { // ref-error {{never produces a constant expression}}
+ constexpr int IgnoredRejected() { // both-error {{never produces a constant expression}}
int n = 0;
sizeof(int[n++]); // both-warning {{expression result unused}} \
- // ref-note 2{{subexpression not valid in a constant expression}}
+ // both-note 2{{subexpression not valid in a constant expression}}
return n;
}
- /// FIXME: This is rejected because the parameter so sizeof() is not constant.
- /// produce a proper diagnostic.
static_assert(IgnoredRejected() == 0, ""); // both-error {{not an integral constant expression}} \
- // ref-note {{in call to 'IgnoredRejected()'}}
+ // both-note {{in call to 'IgnoredRejected()'}}
#endif
#if __cplusplus >= 202002L
/// FIXME: The following code should be accepted.
- consteval int foo(int n) { // ref-error {{consteval function never produces a constant expression}}
- return sizeof(int[n]); // ref-note 3{{not valid in a constant expression}}
- }
- constinit int var = foo(5); // ref-error {{not a constant expression}} \
- // ref-note 2{{in call to}} \
- // ref-error {{does not have a constant initializer}} \
- // ref-note {{required by 'constinit' specifier}} \
- // expected-error {{is not a constant expression}} \
- // expected-error {{does not have a constant initializer}} \
- // expected-note {{required by 'constinit' specifier}} \
+ consteval int foo(int n) { // both-error {{consteval function never produces a constant expression}}
+ return sizeof(int[n]); // both-note 3{{not valid in a constant expression}}
+ }
+ constinit int var = foo(5); // both-error {{not a constant expression}} \
+ // both-note 2{{in call to}} \
+ // both-error {{does not have a constant initializer}} \
+ // both-note {{required by 'constinit' specifier}}
#endif
};
More information about the cfe-commits
mailing list