[clang] [clang][Sema] decay functions to pointers for qualifier discarding and overflow behavior (PR #183616)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 4 06:55:41 PST 2026
https://github.com/NeKon69 updated https://github.com/llvm/llvm-project/pull/183616
>From 689607bea1bc8b23b2bee65be9887686e7d7081b Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Thu, 26 Feb 2026 23:38:45 +0300
Subject: [PATCH 1/3] [clang][Sema] decay functions to pointers for qualifier
discarding and overflow behavior
---
clang/lib/Sema/SemaExpr.cpp | 8 ++++++++
.../CodeGen/incompatible-ptr-function-to-ptr-decay.c | 12 ++++++++++++
2 files changed, 20 insertions(+)
create mode 100644 clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 9cf7f36e12aa6..81d8d3141e72d 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -17531,6 +17531,10 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
// Perform array-to-pointer decay if necessary.
if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType);
+ // Perform function-to-pointer decay if necessary.
+ if (SrcType->isFunctionType())
+ SrcType = Context.getDecayedType(SrcType);
+
isInvalid = true;
Qualifiers lhq = SrcType->getPointeeType().getQualifiers();
@@ -17553,6 +17557,10 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
if (SrcType->isArrayType())
SrcType = Context.getArrayDecayedType(SrcType);
+ // Perform function-to-pointer decay if necessary.
+ if (SrcType->isFunctionType())
+ SrcType = Context.getDecayedType(SrcType);
+
DiagKind = diag::ext_typecheck_convert_discards_overflow_behavior;
break;
case AssignConvertType::CompatiblePointerDiscardsQualifiers:
diff --git a/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c b/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
new file mode 100644
index 0000000000000..1efe53518f7e4
--- /dev/null
+++ b/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
@@ -0,0 +1,12 @@
+// RUN: not %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s
+
+// Issue 182534
+int a();
+
+// CHECK: error: passing 'int (*)()' to parameter of type '__global int (*)' changes address space of pointer
+void b(__attribute__((opencl_global)) int(*) );
+
+void c() {
+ b(a);
+}
+
>From f87be2c7b8d57779818b59fcb03db440af906060 Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Fri, 27 Feb 2026 11:36:22 +0300
Subject: [PATCH 2/3] [clang][Sema] shorten checks and switch to diagnostic
test syntax
---
clang/lib/Sema/SemaExpr.cpp | 14 ++++----------
.../incompatible-ptr-function-to-ptr-decay.c | 5 +++--
2 files changed, 7 insertions(+), 12 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 81d8d3141e72d..1e39ae951700c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -17528,11 +17528,8 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
}
break;
case AssignConvertType::IncompatiblePointerDiscardsQualifiers: {
- // Perform array-to-pointer decay if necessary.
- if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType);
-
- // Perform function-to-pointer decay if necessary.
- if (SrcType->isFunctionType())
+ // Perform decay if necessary.
+ if (SrcType->isArrayType() || SrcType->isFunctionType())
SrcType = Context.getDecayedType(SrcType);
isInvalid = true;
@@ -17554,11 +17551,8 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
// fallthrough
}
case AssignConvertType::IncompatiblePointerDiscardsOverflowBehavior:
- if (SrcType->isArrayType())
- SrcType = Context.getArrayDecayedType(SrcType);
-
- // Perform function-to-pointer decay if necessary.
- if (SrcType->isFunctionType())
+ // Perform decay if necessary.
+ if (SrcType->isArrayType() || SrcType->isFunctionType())
SrcType = Context.getDecayedType(SrcType);
DiagKind = diag::ext_typecheck_convert_discards_overflow_behavior;
diff --git a/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c b/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
index 1efe53518f7e4..385e9e6929f0d 100644
--- a/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
+++ b/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
@@ -1,12 +1,13 @@
-// RUN: not %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s
// Issue 182534
int a();
-// CHECK: error: passing 'int (*)()' to parameter of type '__global int (*)' changes address space of pointer
void b(__attribute__((opencl_global)) int(*) );
+// expected-note at -1 {{passing argument to parameter here}}
void c() {
b(a);
+ // expected-error at -1 {{passing 'int (*)()' to parameter of type '__global int (*)' changes address space of pointer}}
}
>From d7a7df08be2704526af50943c1e1d8fdab285750 Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Fri, 27 Feb 2026 11:53:40 +0300
Subject: [PATCH 3/3] [clang][Sema] use a label in the test
---
clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c b/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
index 385e9e6929f0d..ffeac9f46b194 100644
--- a/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
+++ b/clang/test/CodeGen/incompatible-ptr-function-to-ptr-decay.c
@@ -3,11 +3,11 @@
// Issue 182534
int a();
-void b(__attribute__((opencl_global)) int(*) );
-// expected-note at -1 {{passing argument to parameter here}}
+void b(__attribute__((opencl_global)) int(*) ); // #bdecl
void c() {
b(a);
// expected-error at -1 {{passing 'int (*)()' to parameter of type '__global int (*)' changes address space of pointer}}
+ // expected-note@#bdecl {{passing argument to parameter here}}
}
More information about the cfe-commits
mailing list