Skip to content

Conversation

@mxms0
Copy link
Contributor

@mxms0 mxms0 commented Dec 9, 2025

We dont need to visit or warn on consteval functions as they can't have UB.

We dont need to visit or warn on consteval functions as they can't have UB.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:analysis labels Dec 9, 2025
@mxms0
Copy link
Contributor Author

mxms0 commented Dec 9, 2025

cc @jkorous-apple

@llvmbot
Copy link
Member

llvmbot commented Dec 9, 2025

@llvm/pr-subscribers-clang-analysis

@llvm/pr-subscribers-clang

Author: Max (mxms0)

Changes

We dont need to visit or warn on consteval functions as they can't have UB.


Full diff: https://github.com/llvm/llvm-project/pull/171503.diff

2 Files Affected:

  • (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+5)
  • (modified) clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp (+7)
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

@github-actions
Copy link

github-actions bot commented Dec 9, 2025

🐧 Linux x64 Test Results

  • 112066 tests passed
  • 4571 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 9, 2025

🪟 Windows x64 Test Results

  • 53087 tests passed
  • 2185 tests skipped

✅ The build succeeded and all tests passed.

We dont need to visit or warn on consteval functions as they can't have UB.
@github-actions
Copy link

github-actions bot commented Dec 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@fmayer
Copy link
Contributor

fmayer commented Dec 11, 2025

nit: add [clang] to the description?

@mxms0 mxms0 changed the title [-Wunsafe-buffer-usage] Ignore consteval functions [clang][-Wunsafe-buffer-usage] Ignore consteval functions Dec 12, 2025
@mxms0
Copy link
Contributor Author

mxms0 commented Dec 12, 2025

nit: add [clang] to the description?

Done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants