[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 2 14:34:44 PST 2025


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

`FunctionEffectsRef::get()` is supposed to strip off layers of indirection (pointers/references, type sugar) to get to a `FunctionProtoType` (if any) and return its effects (if any).

It wasn't correctly dealing with situations where the compiler implicitly converts an array to a pointer.

This fixes the immediate bug by checking for array types, though I do wonder if this still isn't sufficient if the array type is wrapped in sugar...

>From 6abcaf1be17d6f8b4412a20347f19972a7b73d91 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] [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();



More information about the cfe-commits mailing list