[clang] [Clang] Ignore assumptions with side effects at compile time (PR #85534)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 16 08:05:33 PDT 2024
https://github.com/Sirraide created https://github.com/llvm/llvm-project/pull/85534
Fixes #85519.
>From 37b7999752cb6bbcf0911b9d6b87f04395a82dcc Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Sat, 16 Mar 2024 16:01:43 +0100
Subject: [PATCH] [Clang] Do not check assumptions with side effects at compile
time
---
clang/lib/AST/ExprConstant.cpp | 3 +++
clang/test/SemaCXX/cxx23-assume.cpp | 10 ++++++++++
2 files changed, 13 insertions(+)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 7137efb7876de2..fa9e8ecf654378 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -5594,6 +5594,9 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
if (Assumption->isValueDependent())
return ESR_Failed;
+ if (Assumption->HasSideEffects(Info.getCtx()))
+ continue;
+
bool Value;
if (!EvaluateAsBooleanCondition(Assumption, Value, Info))
return ESR_Failed;
diff --git a/clang/test/SemaCXX/cxx23-assume.cpp b/clang/test/SemaCXX/cxx23-assume.cpp
index 2d7c9b174d9019..478da092471aff 100644
--- a/clang/test/SemaCXX/cxx23-assume.cpp
+++ b/clang/test/SemaCXX/cxx23-assume.cpp
@@ -126,3 +126,13 @@ static_assert(f5<D>() == 1); // expected-note 3 {{while checking constraint sati
static_assert(f5<double>() == 2);
static_assert(f5<E>() == 1); // expected-note {{while checking constraint satisfaction}} expected-note {{in instantiation of}}
static_assert(f5<F>() == 2); // expected-note {{while checking constraint satisfaction}} expected-note {{in instantiation of}}
+
+// Do not validate assumptions whose evaluation would have side-effects.
+constexpr int foo() {
+ int a = 0;
+ [[assume(a++)]] [[assume(++a)]]; // expected-warning 2 {{has side effects that will be discarded}} ext-warning 2 {{C++23 extension}}
+ [[assume((a+=1))]]; // expected-warning {{has side effects that will be discarded}} ext-warning {{C++23 extension}}
+ return a;
+}
+
+static_assert(foo() == 0);
More information about the cfe-commits
mailing list