llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: mkovacevic99

<details>
<summary>Changes</summary>

Fixes github issue: https://github.com/llvm/llvm-project/issues/118058

This patch fixes handling of deduced types nested inside AtomicType.

Previously, GetContainedDeducedTypeVisitor did not recurse through AtomicType 
nodes, causing getContainedDeducedType() to incorrectly return null for types 
such as:

**_Atomic(__auto_type)**

As a result, undeduced AutoType instances could bypass semantic checks and 
later reach codegen/type layout paths, eventually triggering assertions such as:

_!A-&gt;getDeducedType().isNull() &amp;&amp; "cannot request the size of an 
undeduced or dependent auto type_

This patch adds AtomicType traversal support to GetContainedDeducedTypeVisitor 
so contained deduced types are properly discovered.

Additionally, template deduction logic for AtomicType is extended to allow 
deduction from the atomic value type when the pattern contains an auto type.

CheckOriginalCallArgDeduction is also adjusted to handle atomic wrappers around 
deduced types correctly.

This brings Clang behavior closer to GCC for combinations involving _Atomic and 
__auto_type and prevents undeduced auto types from escaping into later 
compilation stages.

---
Full diff: https://github.com/llvm/llvm-project/pull/197874.diff


3 Files Affected:

- (modified) clang/lib/AST/Type.cpp (+4) 
- (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+16-3) 
- (added) clang/test/Sema/atomic-auto-type.c (+29) 


``````````diff
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 96a398aa21dad..cf42fafab6b9e 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2102,6 +2102,10 @@ class GetContainedDeducedTypeVisitor
   Type *VisitPackExpansionType(const PackExpansionType *T) {
     return Visit(T->getPattern());
   }
+
+  Type *VisitAtomicType(const AtomicType *T) {
+    return Visit(T->getValueType());
+  }
 };
 
 } // namespace
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c04fff6cbd964..78fd5e685cdcd 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -1902,8 +1902,16 @@ static TemplateDeductionResult 
DeduceTemplateArgumentsByTypeMatch(
     //     _Atomic T   [extension]
     case Type::Atomic: {
       const auto *PA = P->castAs<AtomicType>(), *AA = A->getAs<AtomicType>();
-      if (!AA)
-        return TemplateDeductionResult::NonDeducedMismatch;
+      if (!AA) {
+        if (!P->getContainedAutoType())
+          return TemplateDeductionResult::NonDeducedMismatch;
+        // If it contains an auto type, we can try to deduce from the value 
type
+        // of the atomic type.
+        return DeduceTemplateArgumentsByTypeMatch(
+            S, TemplateParams, PA->getValueType(), A, Info, Deduced, TDF,
+            degradeCallPartialOrderingKind(POK),
+            /*DeducedFromArrayBound=*/false, HasDeducedAnyParam);
+      }
       return DeduceTemplateArgumentsByTypeMatch(
           S, TemplateParams, PA->getValueType(), AA->getValueType(), Info,
           Deduced, TDF, degradeCallPartialOrderingKind(POK),
@@ -3744,7 +3752,12 @@ CheckOriginalCallArgDeduction(Sema &S, 
TemplateDeductionInfo &Info,
   QualType OriginalParamType = OriginalArg.OriginalParamType;
 
   // Check for type equality (top-level cv-qualifiers are ignored).
-  if (Context.hasSameUnqualifiedType(A, DeducedA))
+  // If it's an atomic type, we need to check contained value type as well 
since
+  // it can be an auto type.
+  if (Context.hasSameUnqualifiedType(A, DeducedA) ||
+      (DeducedA->isAtomicType() &&
+       Context.hasSameUnqualifiedType(
+           A, cast<AtomicType>(DeducedA)->getValueType())))
     return TemplateDeductionResult::Success;
 
   // Strip off references on the argument types; they aren't needed for
diff --git a/clang/test/Sema/atomic-auto-type.c 
b/clang/test/Sema/atomic-auto-type.c
new file mode 100644
index 0000000000000..245b5621e59f4
--- /dev/null
+++ b/clang/test/Sema/atomic-auto-type.c
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s
+
+// This is a regression test for handling of __auto_type inside _Atomic.
+// Previously this could lead to an undeduced AutoType escaping into
+// ASTContext::getTypeInfoImpl and causing an assertion failure.
+
+int main() {
+  double x = 37;
+
+  __auto_type _Atomic xa = x;
+  _Atomic __auto_type ax = x;
+  
+  _Static_assert(
+      __builtin_types_compatible_p(__typeof(xa), _Atomic double),
+      "incorrect xa type");
+
+  _Static_assert(
+      __builtin_types_compatible_p(__typeof(ax), _Atomic double),
+      "incorrect ax type");
+
+  _Static_assert(
+      __builtin_types_compatible_p(_Atomic double, __typeof(xa)),
+      "incorrect");
+
+  _Static_assert(
+      __builtin_types_compatible_p(_Atomic double, __typeof(ax)),
+      "incorrect");
+  return 0;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/197874
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to