[clang] [clang] Suppress -Wunneeded-internal-declaration for referenced constexpr functions (PR #196593)
Vineet Agarwal via cfe-commits
cfe-commits at lists.llvm.org
Sat May 9 07:58:20 PDT 2026
https://github.com/PlutoDog95 updated https://github.com/llvm/llvm-project/pull/196593
>From d015c1db7fd4716aea18204a7acad502c817f6f4 Mon Sep 17 00:00:00 2001
From: Vineet Agarwal <agarwal.vineet2006 at gmail.com>
Date: Fri, 8 May 2026 22:56:40 +0530
Subject: [PATCH] [clang] Suppress -Wunneeded-internal-declaration for
referenced constexpr functions
Referenced constexpr functions used during constant evaluation may not
be odr-used and therefore do not require emission, but they are still
semantically necessary.
Suppress -Wunneeded-internal-declaration for referenced constexpr
functions, analogous to the existing handling for variables usable in
constant expressions.
Add a regression test for constexpr function usage in a dependent return
type.
---
clang/docs/ReleaseNotes.rst | 4 ++++
clang/lib/Sema/Sema.cpp | 5 ++++
.../warn-unused-constexpr-function.cpp | 24 +++++++++++++++++++
3 files changed, 33 insertions(+)
create mode 100644 clang/test/SemaCXX/warn-unused-constexpr-function.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fa19d4b576575..ec9582de92bf0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -455,6 +455,10 @@ Improvements to Clang's diagnostics
- Clang now emits ``-Wsizeof-pointer-memaccess`` when snprintf/vsnprintf use the sizeof
the destination buffer(dynamically allocated) in the len parameter(#GH162366)
+- Fixed a false positive ``-Wunneeded-internal-declaration`` warning for
+ referenced ``constexpr`` functions used during constant evaluation.
+ (#GH196564)
+
- Added ``-Wmodule-map-path-outside-directory`` (off by default) to warn on
header and umbrella directory paths that use ``..`` to refer outside the module
directory in module maps found via implicit search
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 8a68f2f19bf3d..9c8a84668454a 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -891,6 +891,11 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
return true;
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+ // If a constexpr function is referenced for constant evaluation,
+ // don't warn even if it is not odr-used.
+ if (FD->isReferenced() && FD->isConstexpr())
+ return true;
+
// If this is a function template and none of its specializations is used,
// we should warn.
if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate())
diff --git a/clang/test/SemaCXX/warn-unused-constexpr-function.cpp b/clang/test/SemaCXX/warn-unused-constexpr-function.cpp
new file mode 100644
index 0000000000000..329cfe954c1a6
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unused-constexpr-function.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -std=c++14 -Wunused-function -fsyntax-only %s
+
+static constexpr bool returnInt(int) { return true; }
+
+template <bool B>
+struct select;
+
+template <>
+struct select<true> {
+ using type = int;
+};
+
+template <>
+struct select<false> {
+ using type = float;
+};
+
+template <typename T>
+typename select<returnInt(T{})>::type make() {
+ return T{};
+}
+
+int makeInt() { return make<int>(); }
+float makeFloat() { return make<float>(); }
More information about the cfe-commits
mailing list