[clang] [-Wunsafe-buffer-usage] Ignore consteval functions (PR #171503)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 10 17:51:13 PST 2025
https://github.com/mxms0 updated https://github.com/llvm/llvm-project/pull/171503
>From 6ca667130537476bd4268e15b0f899446f30b6e8 Mon Sep 17 00:00:00 2001
From: mxms <mxms at google.com>
Date: Tue, 9 Dec 2025 20:54:14 +0000
Subject: [PATCH 1/3] [-Wunsafe-buffer-usage] Ignore consteval functions
We dont need to visit or warn on consteval functions as they can't have UB.
---
clang/lib/Analysis/UnsafeBufferUsage.cpp | 5 +++++
clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp | 7 +++++++
2 files changed, 12 insertions(+)
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index da155d31d4a88..893462e3bad58 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -4458,6 +4458,11 @@ void clang::checkUnsafeBufferUsage(const Decl *D,
SmallVector<Stmt *> Stmts;
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
+ // Consteval functions are free of UB by the spec, so we don't need to
+ // visit them or produce diagnostics.
+ if (FD->isConsteval()) {
+ return;
+ }
// We do not want to visit a Lambda expression defined inside a method
// independently. Instead, it should be visited along with the outer method.
// FIXME: do we want to do the same thing for `BlockDecl`s?
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
index 41d38ada48788..6fad7585026f2 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
@@ -406,4 +406,11 @@ void testMultiLineDeclStmt(int * p) {
foo(ap1[1]); // expected-note{{used in buffer access here}}
}
+#if __cplusplus >= 202002L
+consteval void testConstevalPtrArithmetic(int idx) {
+ int y[3] = {0, 1, 2};
+ foo(y[idx]);
+}
+#endif
+
#endif
>From 1e47613d844ded927ccd6f2c6c2831fd615f7d8f Mon Sep 17 00:00:00 2001
From: mxms <mxms at google.com>
Date: Tue, 9 Dec 2025 20:54:14 +0000
Subject: [PATCH 2/3] [-Wunsafe-buffer-usage] Ignore consteval functions
We dont need to visit or warn on consteval functions as they can't have UB.
---
clang/lib/Analysis/UnsafeBufferUsage.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 893462e3bad58..23798becf36a0 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -4460,9 +4460,7 @@ void clang::checkUnsafeBufferUsage(const Decl *D,
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
// Consteval functions are free of UB by the spec, so we don't need to
// visit them or produce diagnostics.
- if (FD->isConsteval()) {
- return;
- }
+ if (FD->isConsteval()) return;
// We do not want to visit a Lambda expression defined inside a method
// independently. Instead, it should be visited along with the outer method.
// FIXME: do we want to do the same thing for `BlockDecl`s?
>From c76782929442d3bf74a777654a2675a6958bea03 Mon Sep 17 00:00:00 2001
From: mxms <mxms at google.com>
Date: Thu, 11 Dec 2025 01:50:49 +0000
Subject: [PATCH 3/3] Fix formatting
---
clang/lib/Analysis/UnsafeBufferUsage.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 23798becf36a0..99c7a44174b3a 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -4460,7 +4460,8 @@ void clang::checkUnsafeBufferUsage(const Decl *D,
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
// Consteval functions are free of UB by the spec, so we don't need to
// visit them or produce diagnostics.
- if (FD->isConsteval()) return;
+ if (FD->isConsteval())
+ return;
// We do not want to visit a Lambda expression defined inside a method
// independently. Instead, it should be visited along with the outer method.
// FIXME: do we want to do the same thing for `BlockDecl`s?
More information about the cfe-commits
mailing list