[clang] [Clang][Sema] Reject array prvalue operands (PR #140702)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 20 03:00:16 PDT 2025
https://github.com/languagelawyer created https://github.com/llvm/llvm-project/pull/140702
of unary `+` and `*`, and binary `+` and `-` operators
Fixes #54016
>From 2914130e2273eb279b978b73f58d5a2d257decb4 Mon Sep 17 00:00:00 2001
From: Andrey Erokhin <language.lawyer at gmail.com>
Date: Tue, 20 May 2025 14:58:23 +0500
Subject: [PATCH] [Clang][Sema] Reject array prvalue operands
of unary + and *, and binary + and - operators
---
clang/lib/Sema/SemaExpr.cpp | 21 +++++++++++++++++++++
clang/test/CXX/expr/p8.cpp | 12 ++++++++++--
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d1889100c382e..c0fa9bc9e895e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11333,6 +11333,11 @@ QualType Sema::CheckAdditionOperands(ExprResult &LHS, ExprResult &RHS,
if (!IExp->getType()->isIntegerType())
return InvalidOperands(Loc, LHS, RHS);
+ if (OriginalOperand Orig(PExp); Orig.getType()->isArrayType() && Orig.Orig->isPRValue()) {
+ Diag(Loc, diag::err_typecheck_array_prvalue_operand) << PExp->getSourceRange();
+ return QualType();
+ }
+
// Adding to a null pointer results in undefined behavior.
if (PExp->IgnoreParenCasts()->isNullPointerConstant(
Context, Expr::NPC_ValueDependentIsNotNull)) {
@@ -11429,6 +11434,16 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS,
return compType;
}
+ OriginalOperand OrigLHS(LHS.get()), OrigRHS(RHS.get());
+ bool LHSArrayPRV = OrigLHS.getType()->isArrayType() && OrigLHS.Orig->isPRValue();
+ bool RHSArrayPRV = OrigRHS.getType()->isArrayType() && OrigRHS.Orig->isPRValue();
+ if (LHSArrayPRV || RHSArrayPRV) {
+ auto&& diag = Diag(Loc, diag::err_typecheck_array_prvalue_operand);
+ if (LHSArrayPRV) diag << LHS.get()->getSourceRange();
+ if (RHSArrayPRV) diag << RHS.get()->getSourceRange();
+ return QualType();
+ }
+
// Either ptr - int or ptr - ptr.
if (LHS.get()->getType()->isAnyPointerType()) {
QualType lpointee = LHS.get()->getType()->getPointeeType();
@@ -15840,6 +15855,12 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
InputExpr->getType()->isSpecificBuiltinType(BuiltinType::Dependent)) {
resultType = Context.DependentTy;
} else {
+ if (Opc == UO_Deref || Opc == UO_Plus) {
+ if (auto *expr = Input.get(); expr->getType()->isArrayType() && expr->isPRValue()) {
+ Diag(OpLoc, diag::err_typecheck_array_prvalue_operand) << expr->getSourceRange();
+ return ExprError();
+ }
+ }
switch (Opc) {
case UO_PreInc:
case UO_PreDec:
diff --git a/clang/test/CXX/expr/p8.cpp b/clang/test/CXX/expr/p8.cpp
index 471d1c5a30206..f736b88b3db09 100644
--- a/clang/test/CXX/expr/p8.cpp
+++ b/clang/test/CXX/expr/p8.cpp
@@ -1,5 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
-// expected-no-diagnostics
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
int a0;
const volatile int a1 = 2;
@@ -16,4 +15,13 @@ int main()
f0(a1);
f1(a2);
f2(a3);
+
+ using IA = int[];
+ void(+IA{ 1, 2, 3 }); // expected-error {{array prvalue}}
+ void(*IA{ 1, 2, 3 }); // expected-error {{array prvalue}}
+ void(IA{ 1, 2, 3 } + 0); // expected-error {{array prvalue}}
+ void(IA{ 1, 2, 3 } - 0); // expected-error {{array prvalue}}
+ void(0 + IA{ 1, 2, 3 }); // expected-error {{array prvalue}}
+ void(0 - IA{ 1, 2, 3 }); // expected-error {{array prvalue}}
+ void(IA{ 1, 2, 3 } - IA{ 1, 2, 3 }); // expected-error {{array prvalue}}
}
More information about the cfe-commits
mailing list