[clang-tools-extra] [llvm] [clang-tidy] Add portability-function-pointer-equality lint (PR #239430)
Jordan R AW via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 15:46:06 PST 2026
https://github.com/ajordanr-google updated https://github.com/llvm/llvm-project/pull/239430
>From e93266af43d10fad4781c0b7f2be56a31eda6390 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] [clang-tidy] Add portability-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-tidy/portability/CMakeLists.txt | 1 +
.../FunctionPointerEqualityCheck.cpp | 116 +++++++++++
.../FunctionPointerEqualityCheck.h | 40 ++++
.../portability/PortabilityTidyModule.cpp | 3 +
clang-tools-extra/docs/ReleaseNotes.md | 8 +
.../docs/clang-tidy/checks/list.md | 1 +
.../portability/function-pointer-equality.md | 89 +++++++++
...function-pointer-equality-custom-keyed.cpp | 24 +++
.../portability/function-pointer-equality.c | 27 +++
.../portability/function-pointer-equality.cpp | 189 ++++++++++++++++++
.../clang-tools-extra/clang-tidy/BUILD.bazel | 1 +
11 files changed, 499 insertions(+)
create mode 100644 clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.cpp
create mode 100644 clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.h
create mode 100644 clang-tools-extra/docs/clang-tidy/checks/portability/function-pointer-equality.md
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality-custom-keyed.cpp
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.c
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.cpp
diff --git a/clang-tools-extra/clang-tidy/portability/CMakeLists.txt b/clang-tools-extra/clang-tidy/portability/CMakeLists.txt
index f9bcb149b81453..32d2a67a19d399 100644
--- a/clang-tools-extra/clang-tidy/portability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/portability/CMakeLists.txt
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangTidyPortabilityModule STATIC
AvoidPragmaCommentCheck.cpp
AvoidPragmaOnceCheck.cpp
+ FunctionPointerEqualityCheck.cpp
NoAssemblerCheck.cpp
PortabilityTidyModule.cpp
RestrictSystemIncludesCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.cpp b/clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.cpp
new file mode 100644
index 00000000000000..fffe96e660931a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.cpp
@@ -0,0 +1,116 @@
+//===----------------------------------------------------------------------===//
+//
+// 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::portability {
+
+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) {
+ const auto CompileTimeOrUnevaluated =
+ anyOf(hasAncestor(expr(matchers::hasUnevaluatedContext())),
+ hasAncestor(staticAssertDecl()), hasAncestor(constantExpr()));
+ // 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()))))),
+ unless(CompileTimeOrUnevaluated))
+ .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 pointers can unexpectedly compare equal "
+ "when linked with '--icf=all', violating the %select{C|C++}0 "
+ "language standard")
+ << getLangOpts().CPlusPlus;
+ diag(MatchedOp->getOperatorLoc(),
+ "consider comparing unique identifiers instead of function pointers",
+ DiagnosticIDs::Note);
+ 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 "
+ "can unexpectedly have key collisions "
+ "when linked with '--icf=all', "
+ "violating the C++ language standard")
+ << MatchedDecl;
+ } else {
+ diag(MatchedDecl->getLocation(), "associative or unordered container "
+ "with a function pointer key type "
+ "can unexpectedly have key collisions "
+ "when linked with '--icf=all', "
+ "violating the C++ language standard");
+ }
+ diag(MatchedDecl->getLocation(),
+ "consider keying on unique identifiers instead of function pointers",
+ DiagnosticIDs::Note);
+ }
+}
+
+void FunctionPointerEqualityCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "KeyedContainerTypes",
+ utils::options::serializeStringList(KeyedContainerTypes));
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.h b/clang-tools-extra/clang-tidy/portability/FunctionPointerEqualityCheck.h
new file mode 100644
index 00000000000000..000e91876635a3
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/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_PORTABILITY_FUNCTIONPOINTEREQUALITYCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_FUNCTIONPOINTEREQUALITYCHECK_H
+
+#include "../ClangTidyCheck.h"
+#include <vector>
+
+namespace clang::tidy::portability {
+
+/// Detects equality comparisons between function pointers and detects
+/// associative or unordered containers with function pointer key types, which
+/// can unexpectedly compare equal or have key collisions respectively when
+/// aggressive Identical Code Folding (``--icf=all``) is enabled.
+///
+/// For the user-facing documentation see:
+/// https://clang.llvm.org/extra/clang-tidy/checks/portability/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::portability
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_FUNCTIONPOINTEREQUALITYCHECK_H
diff --git a/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
index c12ef3d20e871d..92bca3a2efef6f 100644
--- a/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
@@ -10,6 +10,7 @@
#include "../ClangTidyModule.h"
#include "AvoidPragmaCommentCheck.h"
#include "AvoidPragmaOnceCheck.h"
+#include "FunctionPointerEqualityCheck.h"
#include "NoAssemblerCheck.h"
#include "RestrictSystemIncludesCheck.h"
#include "SIMDIntrinsicsCheck.h"
@@ -27,6 +28,8 @@ class PortabilityModule : public ClangTidyModule {
"portability-avoid-pragma-comment");
CheckFactories.registerCheck<AvoidPragmaOnceCheck>(
"portability-avoid-pragma-once");
+ CheckFactories.registerCheck<FunctionPointerEqualityCheck>(
+ "portability-function-pointer-equality");
CheckFactories.registerCheck<NoAssemblerCheck>("portability-no-assembler");
CheckFactories.registerCheck<RestrictSystemIncludesCheck>(
"portability-restrict-system-includes");
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 2549bb564cdd57..7796e1d50f9893 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -190,6 +190,14 @@ infrastructure are described first, followed by tool-specific sections.
Finds uses of `#pragma comment` and, for `lib` or `linker` comments, suggests
using the build system for improved portability.
+- New {doc}`portability-function-pointer-equality
+ <clang-tidy/checks/portability/function-pointer-equality>` check.
+
+ Detects equality comparisons between function pointers and containers keyed
+ by function pointers, which are not compatible with aggressive Identical Code Folding
+ link-time optimizations (`--icf=all`). These comparisons and containers can
+ behave unpredictably and may miscompile when ICF is set to `all`.
+
- New {doc}`readability-redundant-zero-initializer
<clang-tidy/checks/readability/redundant-zero-initializer>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.md b/clang-tools-extra/docs/clang-tidy/checks/list.md
index 35fe8123eb17d0..384e4fdba42f27 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.md
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.md
@@ -382,6 +382,7 @@ readability/*
| {doc}`performance-use-std-move <performance/use-std-move>` | Yes |
| {doc}`portability-avoid-pragma-comment <portability/avoid-pragma-comment>` | |
| {doc}`portability-avoid-pragma-once <portability/avoid-pragma-once>` | |
+| {doc}`portability-function-pointer-equality <portability/function-pointer-equality>` | |
| {doc}`portability-no-assembler <portability/no-assembler>` | |
| {doc}`portability-restrict-system-includes <portability/restrict-system-includes>` | Yes |
| {doc}`portability-simd-intrinsics <portability/simd-intrinsics>` | |
diff --git a/clang-tools-extra/docs/clang-tidy/checks/portability/function-pointer-equality.md b/clang-tools-extra/docs/clang-tidy/checks/portability/function-pointer-equality.md
new file mode 100644
index 00000000000000..8fa5f5f99ebd2b
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/portability/function-pointer-equality.md
@@ -0,0 +1,89 @@
+```{title} clang-tidy - portability-function-pointer-equality
+```
+
+# portability-function-pointer-equality
+
+Detects equality comparisons between function pointers and standard containers
+keyed by function pointers, which can behave unpredictably and may violate
+language standards when aggressive Identical Code Folding (`--icf=all`) is
+enabled.
+
+Identical Code Folding (ICF) is a link-time optimization that merges identical
+code, which saves `.text` size. When linking with `--icf=all`, the linker
+merges distinct functions that have identical machine code into a single copy
+in the final binary, even when their addresses are taken. As a result, two
+distinct function pointers can point to the same address at runtime. Code that
+relies on function pointer comparisons is not compatible with `--icf=all`.
+
+This warns on:
+
+- Equality (`==`) and inequality (`!=`) comparisons between two function
+ pointers, which 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 the code is compatible with `--icf=all`
+link-time optimizations. If the code is not, it could introduce
+miscompilations. For example, the following code may violate the C++ Standard
+with `--icf=all`, as its behavior relies on the uniqueness of function
+addresses.
+
+```c++
+// clang++ -std=c++23 -fuse-ld=lld -O2 -flto=thin -Wl,--icf=all ./main.cpp
+
+void f() {}
+void g() {}
+int main() {
+ volatile auto fAddr = &f;
+ volatile auto gAddr = &g;
+ // Should always return 0, but may incorrectly return 1 if folded.
+ return fAddr == gAddr ? 1 : 0;
+}
+```
+
+**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 pointers can unexpectedly compare equal when linked with
+ // '--icf=all', violating the C++ language standard
+ // Note: consider comparing unique identifiers instead of function pointers
+ return f == g;
+}
+
+// Warning: associative or unordered container 'callbacks' with a function
+// pointer key type can unexpectedly have key collisions when linked with
+// '--icf=all', violating the C++ language standard
+// Note: consider keying on unique identifiers instead of function pointers
+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/portability/function-pointer-equality-custom-keyed.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality-custom-keyed.cpp
new file mode 100644
index 00000000000000..d5757a3d3458ff
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality-custom-keyed.cpp
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s portability-function-pointer-equality %t -- \
+// RUN: -config='{CheckOptions: { portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ // CHECK-MESSAGES: :[[@LINE-2]]:38: note: consider keying on unique identifiers instead of function pointers
+
+ std::set<void (*)()> s;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.c b/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.c
new file mode 100644
index 00000000000000..dc5ede805070e3
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.c
@@ -0,0 +1,27 @@
+// RUN: %check_clang_tidy %s portability-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 pointers can unexpectedly compare equal when linked with '--icf=all', violating the C language standard [portability-function-pointer-equality]
+ // CHECK-MESSAGES: :[[@LINE-3]]:9: note: consider comparing unique identifiers instead of function pointers
+ if (fp1 != fp2)
+ return 2;
+ // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C language standard [portability-function-pointer-equality]
+ // CHECK-MESSAGES: :[[@LINE-3]]:11: note: consider comparing unique identifiers instead of function pointers
+
+ 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/portability/function-pointer-equality.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.cpp
new file mode 100644
index 00000000000000..e3d09df3eadbba
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/portability/function-pointer-equality.cpp
@@ -0,0 +1,189 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s portability-function-pointer-equality %t
+
+#include <map>
+#include <set>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+void f();
+void g();
+
+void test_equality_static() {
+ bool b1 = (f == g);
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ // CHECK-MESSAGES: :[[@LINE-2]]:16: note: consider comparing unique identifiers instead of function pointers
+ bool b2 = (f != g);
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+}
+
+void test_equality(void (*fp1)(), void (*fp2)()) {
+ bool b3 = (fp1 == fp2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ bool b4 = (fp1 != fp2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ bool b5 = (fp1 == f);
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+
+ void (&rf)() = f;
+ void (&rg)() = g;
+ bool b6 = (rf == rg);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-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 pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ bool b1 = &Foo::MyMethod1 != &Foo::MyMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ bool a2 = &Foo::MyStaticMethod1 == &Foo::MyStaticMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ bool b2 = &Foo::MyStaticMethod1 != &Foo::MyStaticMethod2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ bool c1 = &Foo::MyMethod1 == nullptr;
+}
+
+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);
+}
+
+void test_unevaluated() {
+ // Unevaluated/compile-time contexts cannot be affected by link-time ICF.
+ static_assert(f != (g));
+ static_assert((&f) != &g);
+ sizeof(f == g);
+ decltype(f != g);
+ if constexpr (f == g) {
+ void;
+ }
+}
+
+// Container code ---------
+
+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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ // CHECK-MESSAGES: :[[@LINE-2]]:24: note: consider keying on unique identifiers instead of function pointers
+ std::multiset<void (*)()> b1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: associative or unordered container 'b1' with a function pointer key type can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ unordered_set<void (*)()> s4;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: associative or unordered container 's4' with a function pointer key type can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ map<void (*)(), int> m3;
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: associative or unordered container 'm3' with a function pointer key type can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+// CHECK-MESSAGES: :[[@LINE-2]]:45: note: consider keying on unique identifiers instead of function pointers
+
+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 pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-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 can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+}
+
+template <typename T>
+void non_dependent_in_template() {
+ bool b = (f == g);
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function pointers can unexpectedly compare equal when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+ std::set<void (*)()> s;
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: associative or unordered container 's' with a function pointer key type can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+}
+
+void test_templates() {
+ generic_eq(f, g);
+ generic_set<void (*)()>();
+ non_dependent_in_template<int>();
+ non_dependent_in_template<double>();
+}
+
+void test_member_functions() {
+ std::set<void (Foo::*)()> set;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: associative or unordered container 'set' with a function pointer key type can unexpectedly have key collisions when linked with '--icf=all', violating the C++ language standard [portability-function-pointer-equality]
+}
diff --git a/utils/bazel/llvm-project-overlay/clang-tools-extra/clang-tidy/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang-tools-extra/clang-tidy/BUILD.bazel
index eb8701823beda3..da02801fcf66e9 100644
--- a/utils/bazel/llvm-project-overlay/clang-tools-extra/clang-tidy/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang-tools-extra/clang-tidy/BUILD.bazel
@@ -135,6 +135,7 @@ clang_tidy_library(
name = "portability",
deps = [
":lib",
+ ":utils",
"//llvm:TargetParser",
],
)
More information about the cfe-commits
mailing list