[clang-tools-extra] [clang-tidy] Add misc-function-pointer-equality lint (PR #239430)
Jordan R AW via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 13:46:39 PST 2026
https://github.com/ajordanr-google updated https://github.com/llvm/llvm-project/pull/239430
>From aba779963d9a0ba2521d996a25dacf0f09729622 Mon Sep 17 00:00:00 2001
From: Jordan R Abrahams-Whitehead <ajordanr at google.com>
Date: Wed, 7 Oct 2026 18:27:23 +0000
Subject: [PATCH 1/2] [clang-tidy] Add misc-function-pointer-equality lint
This commit introduces a brand new clang-tidy lint to help detect
miscompiles when adding the linker flag --icf=all. Aggressive
identical code folding when function addresses are taken can lead
to function pointer comparisons that violate the C++ language
specification, and thus lead to incorrect runtime behaviour.
However, we can check for many of the common instances where these
function pointer comparsions occur, and warn on them. We unfortunately
can't tell if the user is building with `--icf=all`, but in practice
this code pattern seems extremely rare in large code bases (and in
some cases, is unspecified behaviour even without any code folding).
This code was written by me, though I used some AI assistance to ensure
I didn't miss updating docs, and making sure I was running the right
tests.
---
clang-tools-extra/clang-tidy/.clang-tidy | 1 +
.../clang-tidy/misc/CMakeLists.txt | 1 +
.../misc/FunctionPointerEqualityCheck.cpp | 101 +++++++++++++++++
.../misc/FunctionPointerEqualityCheck.h | 40 +++++++
.../clang-tidy/misc/MiscTidyModule.cpp | 3 +
clang-tools-extra/docs/ReleaseNotes.md | 7 ++
.../docs/clang-tidy/checks/list.md | 1 +
.../checks/misc/function-pointer-equality.md | 67 ++++++++++++
.../misc/function-pointer-container-keys.cpp | 102 ++++++++++++++++++
.../misc/function-pointer-custom-keyed.cpp | 24 +++++
.../checkers/misc/function-pointer-equality.c | 25 +++++
.../misc/function-pointer-equality.cpp | 69 ++++++++++++
12 files changed, 441 insertions(+)
create mode 100644 clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp
create mode 100644 clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.h
create mode 100644 clang-tools-extra/docs/clang-tidy/checks/misc/function-pointer-equality.md
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-container-keys.cpp
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-custom-keyed.cpp
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.c
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp
diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy
index 972c25c2be5c5..235feb8d27281 100644
--- a/clang-tools-extra/clang-tidy/.clang-tidy
+++ b/clang-tools-extra/clang-tidy/.clang-tidy
@@ -28,6 +28,7 @@ Checks: [
'-modernize-use-trailing-return-type',
'performance-*',
'-performance-enum-size',
+ '-performance-function-pointer-equality',
'-performance-no-int-to-ptr',
'readability-*',
'-readability-avoid-nested-conditional-operator',
diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
index fdfae8966b363..7c065604fbf42 100644
--- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -24,6 +24,7 @@ add_clang_library(clangTidyMiscModule STATIC
CoroutineHostileRAIICheck.cpp
DefinitionsInHeadersCheck.cpp
ExplicitConstructorCheck.cpp
+ FunctionPointerEqualityCheck.cpp
HeaderIncludeCycleCheck.cpp
IncludeCleanerCheck.cpp
MiscTidyModule.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp b/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp
new file mode 100644
index 0000000000000..3db4a14170e34
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp
@@ -0,0 +1,101 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "FunctionPointerEqualityCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+FunctionPointerEqualityCheck::FunctionPointerEqualityCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ KeyedContainerTypes(utils::options::parseStringList(
+ Options.get("KeyedContainerTypes", "::std::flat_map;"
+ "::std::flat_set;"
+ "::std::map;"
+ "::std::multimap;"
+ "::std::multiset;"
+ "::std::set;"
+ "::std::flat_multimap;"
+ "::std::flat_multiset;"
+ "::std::unordered_map;"
+ "::std::unordered_multimap;"
+ "::std::unordered_multiset;"
+ "::std::unordered_set"))) {}
+
+void FunctionPointerEqualityCheck::registerMatchers(MatchFinder *Finder) {
+ // Matching ptr == ptr, but not ptr == nullptr, or ptr == integer (function
+ // pointers to 1 and -1 are frequently used for "invalid" function pointers).
+ // Also match equality comparisons of member pointers that point to
+ // functions.
+ const auto FunctionPointerType = hasCanonicalType(anyOf(
+ pointsTo(functionType()), memberPointerType(pointee(functionType()))));
+ Finder->addMatcher(
+ binaryOperator(hasAnyOperatorName("==", "!="),
+ hasLHS(hasType(FunctionPointerType)),
+ hasRHS(hasType(FunctionPointerType)),
+ unless(hasEitherOperand(ignoringParenCasts(
+ anyOf(nullPointerConstant(), hasType(isInteger()))))))
+ .bind("op"),
+ this);
+
+ // Note, that while some of these containers check equality through the
+ // `Compare` requirement, the `Compare` requirement mandates strict weak
+ // ordering, which is unspecified behaviour or implementation defined
+ // anyways when they don't default to std::less.
+ const auto ContainerTypesMatcher =
+ matchers::matchesAnyListedRegexName(KeyedContainerTypes);
+ Finder->addMatcher(
+ declaratorDecl(hasType(hasCanonicalType(recordType(
+ hasDeclaration(classTemplateSpecializationDecl(
+ ContainerTypesMatcher,
+ hasTemplateArgument(
+ 0, refersToType(FunctionPointerType))))))))
+ .bind("var"),
+ this);
+}
+
+void FunctionPointerEqualityCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ if (const auto *MatchedOp = Result.Nodes.getNodeAs<BinaryOperator>("op")) {
+ // Skip if either type is of type decltype(nullptr). This is a builtin
+ // type.
+ if (MatchedOp->getLHS()->IgnoreParenCasts()->getType()->isNullPtrType() ||
+ MatchedOp->getRHS()->IgnoreParenCasts()->getType()->isNullPtrType()) {
+ return;
+ }
+ diag(MatchedOp->getOperatorLoc(),
+ "function pointer equality comparison is unsound with '--icf=all' "
+ "enabled");
+ return;
+ }
+ if (const auto *MatchedDecl = Result.Nodes.getNodeAs<DeclaratorDecl>("var")) {
+ if (MatchedDecl->getDeclName()) {
+ diag(MatchedDecl->getLocation(), "associative or unordered container %0 "
+ "with a function pointer key type "
+ "is unsound with '--icf=all' enabled")
+ << MatchedDecl;
+ } else {
+ diag(MatchedDecl->getLocation(), "associative or unordered container "
+ "with a function pointer key type is "
+ "unsound with '--icf=all' enabled");
+ }
+ }
+}
+
+void FunctionPointerEqualityCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "KeyedContainerTypes",
+ utils::options::serializeStringList(KeyedContainerTypes));
+}
+
+} // namespace clang::tidy::misc
diff --git a/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.h b/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.h
new file mode 100644
index 0000000000000..f333a81a55950
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONPOINTEREQUALITYCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONPOINTEREQUALITYCHECK_H
+
+#include "../ClangTidyCheck.h"
+#include <vector>
+
+namespace clang::tidy::misc {
+
+/// Detects equality comparisons between function pointers and detects
+/// associative or unordered containers with function pointer key types, which
+/// can be unsound when aggressive Identical Code Folding (``--icf=all``) is
+/// enabled.
+///
+/// For the user-facing documentation see:
+/// https://clang.llvm.org/extra/clang-tidy/checks/misc/function-pointer-equality.html
+class FunctionPointerEqualityCheck : public ClangTidyCheck {
+public:
+ FunctionPointerEqualityCheck(StringRef Name, ClangTidyContext *Context);
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+
+private:
+ /// Semicolon-separated fully-qualified container names (e.g.
+ /// ``::std::set;::std::map``) to warn on the first template argument for
+ /// being a function pointer type key.
+ const std::vector<StringRef> KeyedContainerTypes;
+};
+
+} // namespace clang::tidy::misc
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONPOINTEREQUALITYCHECK_H
diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
index 52d3b4297ba26..a10eecb45b59b 100644
--- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -14,6 +14,7 @@
#include "CoroutineHostileRAIICheck.h"
#include "DefinitionsInHeadersCheck.h"
#include "ExplicitConstructorCheck.h"
+#include "FunctionPointerEqualityCheck.h"
#include "HeaderIncludeCycleCheck.h"
#include "IncludeCleanerCheck.h"
#include "MisleadingBidirectionalCheck.h"
@@ -57,6 +58,8 @@ class MiscModule : public ClangTidyModule {
"misc-definitions-in-headers");
CheckFactories.registerCheck<ExplicitConstructorCheck>(
"misc-explicit-constructor");
+ CheckFactories.registerCheck<FunctionPointerEqualityCheck>(
+ "misc-function-pointer-equality");
CheckFactories.registerCheck<HeaderIncludeCycleCheck>(
"misc-header-include-cycle");
CheckFactories.registerCheck<IncludeCleanerCheck>("misc-include-cleaner");
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 4cd6b4b8a82ee..9a0edd7466fe0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -150,6 +150,13 @@ infrastructure are described first, followed by tool-specific sections.
Detects malformed regex patterns defined in a single string literal.
+- New {doc}`misc-function-pointer-equality
+ <clang-tidy/checks/misc/function-pointer-equality>` check.
+
+ Detects equality comparisons between function pointers and containers keyed
+ by function pointers, which can behave unpredictably and unsoundly when
+ aggressive Identical Code Folding (`--icf=all`) is enabled.
+
- New {doc}`modernize-use-if-consteval
<clang-tidy/checks/modernize/use-if-consteval>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.md b/clang-tools-extra/docs/clang-tidy/checks/list.md
index ec20c38013185..4311b7d09090a 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.md
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.md
@@ -270,6 +270,7 @@ readability/*
| {doc}`misc-coroutine-hostile-raii <misc/coroutine-hostile-raii>` | |
| {doc}`misc-definitions-in-headers <misc/definitions-in-headers>` | Yes |
| {doc}`misc-explicit-constructor <misc/explicit-constructor>` | Yes |
+| {doc}`misc-function-pointer-equality <misc/function-pointer-equality>` | |
| {doc}`misc-header-include-cycle <misc/header-include-cycle>` | |
| {doc}`misc-include-cleaner <misc/include-cleaner>` | Yes |
| {doc}`misc-misleading-bidirectional <misc/misleading-bidirectional>` | |
diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/function-pointer-equality.md b/clang-tools-extra/docs/clang-tidy/checks/misc/function-pointer-equality.md
new file mode 100644
index 0000000000000..f8e6756dafcec
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/function-pointer-equality.md
@@ -0,0 +1,67 @@
+```{title} clang-tidy - misc-function-pointer-equality
+```
+
+# misc-function-pointer-equality
+
+Detects equality comparisons between function pointers and standard containers
+keyed by function pointers, which can behave unpredictably or unsoundly when
+aggressive Identical Code Folding (`--icf=all`) is enabled.
+
+For example, when linking with `--icf=all`, the linker merges distinct
+functions that have identical machine code into a single copy in the final
+binary. As a result, two distinct functions can share the same address at
+runtime.
+
+This warns on:
+
+- Equality (`==`) and inequality (`!=`) comparisons between two function
+ pointers may evaluate unexpectedly.
+- Various associative and unordered standard library containers (e.g.
+ `std::set`, `std::multiset`, or `std::map`) that are keyed by
+ function pointers rely on address ordering or equality, which can cause
+ distinct functions to collide as equivalent keys or produce non-deterministic
+ ordering. This can be configured with `KeyedContainerTypes`.
+
+This check exists to determine if `--icf=all` could be introducing miscompiles
+in some common cases. **If you do not ever intend to enable `--icf=all`, you
+can safely ignore this check.**
+
+Some examples of what is linted:
+
+```c++
+#include <unordered_map>
+
+void f();
+void g();
+
+bool compare() {
+ // Warning: function pointer equality comparison is unsound with '--icf=all'
+ // enabled.
+ return f == g;
+}
+
+// Warning: container 'callbacks' with function pointer key is unsound
+// with '--icf=all' enabled.
+std::unordered_map<void (*)(), int> callbacks;
+```
+
+Comparisons against null pointer constants (`nullptr`, `NULL`, `0`) or `nullptr_t`
+are not flagged.
+
+```c++
+bool is_null(void (*fp)()) {
+ return fp == nullptr;
+}
+```
+
+## Options
+
+```{option} KeyedContainerTypes
+
+Semicolon-separated list of container regex patterns to check for whether their
+first template argument is a function pointer. For many associative and
+associative-like containers, this is the key type. Default is
+`::std::flat_map;::std::flat_set;::std::map;::std::multimap;::std::multiset;::std::set;::std::flat_multimap;::std::flat_multiset;::std::unordered_map;::std::unordered_multimap;::std::unordered_multiset;::std::unordered_set`
+```
+
+
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-container-keys.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-container-keys.cpp
new file mode 100644
index 0000000000000..036ef357ae199
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-container-keys.cpp
@@ -0,0 +1,102 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s misc-function-pointer-equality %t
+
+#include <map>
+#include <set>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+void f();
+void g();
+
+void test_containers_std() {
+ std::set<void (*)()> a1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: associative or unordered container 'a1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::multiset<void (*)()> b1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: associative or unordered container 'b1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::unordered_set<void (*)()> c1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: associative or unordered container 'c1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::unordered_multiset<void (*)()> d1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: associative or unordered container 'd1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::map<void (*)(), int> e1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: associative or unordered container 'e1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::multimap<void (*)(), int> f1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: associative or unordered container 'f1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::unordered_map<void (*)(), int> g1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: associative or unordered container 'g1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::unordered_multimap<void (*)(), int> h1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: associative or unordered container 'h1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ const std::set<void (*)()> i1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: associative or unordered container 'i1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ // Test `using` directive to rename a type.
+ using FnMap = std::unordered_map<void (*)(), int>;
+ FnMap j1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: associative or unordered container 'j1' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ // Should not lint (either because they don't have a function pointer key, or are not actually a container).
+ std::map<int, void (*)()> k1;
+ std::unordered_map<int, void (*)()> l1;
+ const std::set<void (*)()> &my_ref = i1;
+ const std::set<void (*)()> *my_ptr = &i1;
+}
+
+namespace std
+{
+void test_containers_namespace() {
+ set<void (*)()> s3;
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: associative or unordered container 's3' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ unordered_set<void (*)()> s4;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: associative or unordered container 's4' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ map<void (*)(), int> m3;
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: associative or unordered container 'm3' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ unordered_map<void (*)(), int> m4;
+ // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: associative or unordered container 'm4' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ vector<void (*)()> v2;
+ map<int, void (*)()> m_val2;
+}
+}
+
+struct S {
+ std::set<void (*)()> member;
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: associative or unordered container 'member' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+};
+
+std::set<void (*)()> global_set;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: associative or unordered container 'global_set' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+void test_param(std::set<void (*)()> param_set) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: associative or unordered container 'param_set' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+void test_unnamed_param(std::set<void (*)()>);
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: associative or unordered container with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+std::set<void (*)()> returns_set();
+
+template <typename T>
+bool generic_eq(T a, T b) {
+ return a == b;
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+}
+
+template <typename T>
+void generic_set() {
+ std::set<T> s;
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: associative or unordered container 's' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+}
+
+template <typename T>
+void non_dependent_in_template() {
+ bool b = (f == g);
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ std::set<void (*)()> s;
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: associative or unordered container 's' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+}
+
+void test_templates() {
+ generic_eq(f, g);
+ generic_set<void (*)()>();
+ non_dependent_in_template<int>();
+ non_dependent_in_template<double>();
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-custom-keyed.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-custom-keyed.cpp
new file mode 100644
index 0000000000000..1bff2b755c321
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-custom-keyed.cpp
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s misc-function-pointer-equality %t -- \
+// RUN: -config='{CheckOptions: { misc-function-pointer-equality.KeyedContainerTypes: "::std::MyCustomContainer" }}'
+
+#include <set>
+
+void f();
+
+namespace std
+{
+template<typename T>
+class MyCustomContainer {
+public:
+ MyCustomContainer(T myVal): val(myVal) {};
+ T val;
+};
+}
+
+void test_custom_container() {
+ std::MyCustomContainer<void (*)()> a(&f);
+ // CHECK-MESSAGES: :[[@LINE-1]]:38: warning: associative or unordered container 'a' with a function pointer key type is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ std::set<void (*)()> s;
+}
+
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.c b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.c
new file mode 100644
index 0000000000000..c6302b11dc716
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.c
@@ -0,0 +1,25 @@
+// RUN: %check_clang_tidy %s misc-function-pointer-equality %t
+
+#define NULL ((void *)0)
+
+void f(void);
+void g(void);
+
+typedef void (*fn_ptr)(void);
+
+int test_c_equality(fn_ptr fp1, fn_ptr fp2) {
+ if (f == g)
+ return 1;
+ // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ if (fp1 != fp2)
+ return 2;
+ // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ if (f == (void *) 0 || fp1 == 0)
+ return 3;
+
+ if (f == (void *) 1 || fp1 == ((void *) -1))
+ return 3;
+
+ return 0;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp
new file mode 100644
index 0000000000000..2d7bd9ee2c9eb
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp
@@ -0,0 +1,69 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s misc-function-pointer-equality %t
+
+void f();
+void g();
+
+void test_equality_static() {
+ bool b1 = (f == g);
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ bool b2 = (f != g);
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+}
+
+void test_equality(void (*fp1)(), void (*fp2)()) {
+ bool b3 = (fp1 == fp2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ bool b4 = (fp1 != fp2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ bool b5 = (fp1 == f);
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ void (&rf)() = f;
+ void (&rg)() = g;
+ bool b6 = (rf == rg);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+
+ // Null pointer comparisons are allowed.
+ bool n1 = (f == nullptr);
+ bool n2 = (f != nullptr);
+ bool n3 = (nullptr == fp1);
+ bool n4 = (fp1 != 0);
+ bool n5 = (fp1 == static_cast<void (*)()>(nullptr));
+}
+
+class Foo
+{
+public:
+ int MyMethod1() {
+ return 1;
+ }
+ int MyMethod2() {
+ return 2;
+ }
+ static void MyStaticMethod1() {
+ return;
+ }
+ static void MyStaticMethod2() {
+ return;
+ }
+};
+
+void test_member_function_equality() {
+ bool a1 = &Foo::MyMethod1 == &Foo::MyMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ bool b1 = &Foo::MyMethod1 != &Foo::MyMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ bool a2 = &Foo::MyStaticMethod1 == &Foo::MyStaticMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+ bool b2 = &Foo::MyStaticMethod1 != &Foo::MyStaticMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: function pointer equality comparison is unsound with '--icf=all' enabled [misc-function-pointer-equality]
+}
+
+bool weird_nullptr_edgecase(decltype(nullptr) a, void (*b)()) {
+ // Should not report an issue, as `a` is a nullptr.
+ return a == b;
+}
+
+void test_nullptr_t() {
+ weird_nullptr_edgecase(nullptr, &f);
+}
>From 5b87c53076562482f194d973dba459be5fa2fd77 Mon Sep 17 00:00:00 2001
From: Jordan R Abrahams-Whitehead <ajordanr at google.com>
Date: Fri, 13 Nov 2026 21:46:25 +0000
Subject: [PATCH 2/2] fixup! [clang-tidy] Add misc-function-pointer-equality
lint
---
.../clang-tidy/misc/FunctionPointerEqualityCheck.cpp | 6 +++++-
.../checkers/misc/function-pointer-equality.cpp | 8 ++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp b/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp
index 3db4a14170e34..bcb47b1d10e8e 100644
--- a/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/FunctionPointerEqualityCheck.cpp
@@ -33,6 +33,9 @@ FunctionPointerEqualityCheck::FunctionPointerEqualityCheck(
"::std::unordered_set"))) {}
void FunctionPointerEqualityCheck::registerMatchers(MatchFinder *Finder) {
+ const auto CompileTimeOrUnevaluated =
+ anyOf(hasAncestor(expr(matchers::hasUnevaluatedContext())),
+ hasAncestor(staticAssertDecl()));
// Matching ptr == ptr, but not ptr == nullptr, or ptr == integer (function
// pointers to 1 and -1 are frequently used for "invalid" function pointers).
// Also match equality comparisons of member pointers that point to
@@ -44,7 +47,8 @@ void FunctionPointerEqualityCheck::registerMatchers(MatchFinder *Finder) {
hasLHS(hasType(FunctionPointerType)),
hasRHS(hasType(FunctionPointerType)),
unless(hasEitherOperand(ignoringParenCasts(
- anyOf(nullPointerConstant(), hasType(isInteger()))))))
+ anyOf(nullPointerConstant(), hasType(isInteger()))))),
+ unless(CompileTimeOrUnevaluated))
.bind("op"),
this);
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp
index 2d7bd9ee2c9eb..ffa3239c3035a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/function-pointer-equality.cpp
@@ -67,3 +67,11 @@ bool weird_nullptr_edgecase(decltype(nullptr) a, void (*b)()) {
void test_nullptr_t() {
weird_nullptr_edgecase(nullptr, &f);
}
+
+void test_static_assert() {
+ // Can never lead to unsoundness due to code folding.
+ static_assert(f != (g));
+ static_assert((&f) != &g);
+ sizeof(f == g);
+ decltype(f != g);
+}
More information about the cfe-commits
mailing list