[clang] eb82ddc - [clang][-Wunsafe-buffer-usage] Ignore consteval functions (#171503)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 15 09:42:11 PST 2026
Author: Max
Date: 2026-01-15T12:42:05-05:00
New Revision: eb82ddc53a21ee7a8acf4de9efe1e385c88fe497
URL: https://github.com/llvm/llvm-project/commit/eb82ddc53a21ee7a8acf4de9efe1e385c88fe497
DIFF: https://github.com/llvm/llvm-project/commit/eb82ddc53a21ee7a8acf4de9efe1e385c88fe497.diff
LOG: [clang][-Wunsafe-buffer-usage] Ignore consteval functions (#171503)
We dont need to visit or warn on consteval functions as they can't have
UB.
---------
Co-authored-by: mxms <mxms at google.com>
Added:
Modified:
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index a50a284a37566..6bb08102c0345 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -4594,6 +4594,10 @@ 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
More information about the cfe-commits
mailing list