[clang] [Clang] FunctionEffects: Correctly navigate through array types in FunctionEffectsRef::get(). (PR #121525)

Doug Wyatt via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 16 11:06:35 PST 2025


https://github.com/dougsonos updated https://github.com/llvm/llvm-project/pull/121525

>From 805b182e1a9ebe5e344943748dce6c86594495eb Mon Sep 17 00:00:00 2001
From: Doug Wyatt <dwyatt at apple.com>
Date: Thu, 2 Jan 2025 14:28:48 -0800
Subject: [PATCH 1/3] [Clang] FunctionEffects: Correctly navigate through array
 types in FunctionEffectsRef::get().

---
 clang/include/clang/AST/Type.h                  | 17 +++++++++++++----
 .../test/Sema/attr-nonblocking-constraints.cpp  | 10 ++++++++++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 09c98f642852fc..782c32f41852e2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -8836,13 +8836,22 @@ void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
                              unsigned Scale);
 
 inline FunctionEffectsRef FunctionEffectsRef::get(QualType QT) {
+  const Type *TypePtr = QT.getTypePtr();
   while (true) {
-    QualType Pointee = QT->getPointeeType();
-    if (Pointee.isNull())
+    // Note that getPointeeType() seems to successfully navigate some constructs
+    // for which isAnyPointerType() returns false (e.g.
+    // pointer-to-member-function).
+    QualType Pointee = TypePtr->getPointeeType();
+    if (Pointee.isNull()) {
+      if (TypePtr->isArrayType()) {
+        TypePtr = TypePtr->getBaseElementTypeUnsafe();
+        continue;
+      }
       break;
-    QT = Pointee;
+    }
+    TypePtr = Pointee.getTypePtr();
   }
-  if (const auto *FPT = QT->getAs<FunctionProtoType>())
+  if (const auto *FPT = TypePtr->getAs<FunctionProtoType>())
     return FPT->getFunctionEffects();
   return {};
 }
diff --git a/clang/test/Sema/attr-nonblocking-constraints.cpp b/clang/test/Sema/attr-nonblocking-constraints.cpp
index bbc909f627f4c3..8304a38f9af500 100644
--- a/clang/test/Sema/attr-nonblocking-constraints.cpp
+++ b/clang/test/Sema/attr-nonblocking-constraints.cpp
@@ -246,6 +246,16 @@ void PTMFTester::convert() [[clang::nonblocking]]
 	(this->*mConvertFunc)();
 }
 
+// Allow implicit conversion from array to pointer.
+void nb14(unsigned idx) [[clang::nonblocking]]
+{
+	using FP = void (*)() [[clang::nonblocking]];
+	auto nb = +[]() [[clang::nonblocking]] {};
+
+	FP array[4] = { nb, nb, nb, nb };
+	FP f = array[idx]; // This should not generate a warning.
+}
+
 // Block variables
 void nb17(void (^blk)() [[clang::nonblocking]]) [[clang::nonblocking]] {
 	blk();

>From 9dc1d8627d5f9ab4bfc6106161347d1d15063baf Mon Sep 17 00:00:00 2001
From: Doug Wyatt <dwyatt at apple.com>
Date: Thu, 2 Jan 2025 16:55:11 -0800
Subject: [PATCH 2/3] Test an array hidden behind a typealias.

---
 clang/test/Sema/attr-nonblocking-constraints.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/test/Sema/attr-nonblocking-constraints.cpp b/clang/test/Sema/attr-nonblocking-constraints.cpp
index 8304a38f9af500..f7b5abbfa34e91 100644
--- a/clang/test/Sema/attr-nonblocking-constraints.cpp
+++ b/clang/test/Sema/attr-nonblocking-constraints.cpp
@@ -250,10 +250,11 @@ void PTMFTester::convert() [[clang::nonblocking]]
 void nb14(unsigned idx) [[clang::nonblocking]]
 {
 	using FP = void (*)() [[clang::nonblocking]];
+	using FPArray = FP[2];
 	auto nb = +[]() [[clang::nonblocking]] {};
 
-	FP array[4] = { nb, nb, nb, nb };
-	FP f = array[idx]; // This should not generate a warning.
+	FPArray src{ nb, nullptr };
+	FP f = src[idx]; // This should not generate a warning.
 }
 
 // Block variables

>From f16a0128dded93c3e283f285f7ac1c50b4f2ce0e Mon Sep 17 00:00:00 2001
From: Doug Wyatt <doug at sonosphere.com>
Date: Thu, 16 Jan 2025 11:06:25 -0800
Subject: [PATCH 3/3] Update clang/include/clang/AST/Type.h

Co-authored-by: Sirraide <aeternalmail at gmail.com>
---
 clang/include/clang/AST/Type.h | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 782c32f41852e2..e1e960e6706b37 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -8838,15 +8838,11 @@ void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
 inline FunctionEffectsRef FunctionEffectsRef::get(QualType QT) {
   const Type *TypePtr = QT.getTypePtr();
   while (true) {
-    // Note that getPointeeType() seems to successfully navigate some constructs
-    // for which isAnyPointerType() returns false (e.g.
-    // pointer-to-member-function).
-    QualType Pointee = TypePtr->getPointeeType();
-    if (Pointee.isNull()) {
-      if (TypePtr->isArrayType()) {
-        TypePtr = TypePtr->getBaseElementTypeUnsafe();
-        continue;
-      }
+    if (QualType Pointee = TypePtr->getPointeeType())
+      TypePtr = Pointee.getTypePtr();
+    else if (TypePtr->isArrayType())
+      TypePtr = TypePtr->getBaseElementTypeUnsafe();
+    else
       break;
     }
     TypePtr = Pointee.getTypePtr();



More information about the cfe-commits mailing list