[clang] [APINotes] Avoid duplicate attributes when fields instantiate class templates (PR #173386)
Susana Monteiro via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 23 08:11:56 PST 2025
https://github.com/susmonteiro updated https://github.com/llvm/llvm-project/pull/173386
>From 65c3fb33820ab020a91a27579b4937e5a1340e4b Mon Sep 17 00:00:00 2001
From: susmonteiro <susmonteiro at apple.com>
Date: Tue, 23 Dec 2025 14:56:05 +0000
Subject: [PATCH] [APINotes] Avoid duplicate attributes when fields instantiate
class templates
---
clang/lib/Sema/SemaDecl.cpp | 3 ++-
.../Inputs/Headers/Templates.apinotes | 2 ++
.../test/APINotes/Inputs/Headers/Templates.h | 14 ++++++++++++
clang/test/APINotes/templates.cpp | 22 +++++++++++++++++++
4 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5da665a5acad2..11323803e1910 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20161,7 +20161,8 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
CDecl->setIvarRBraceLoc(RBrac);
}
}
- ProcessAPINotes(Record);
+ if (Record && !isa<ClassTemplateSpecializationDecl>(Record))
+ ProcessAPINotes(Record);
}
// Given an integral type, return the next larger integral type
diff --git a/clang/test/APINotes/Inputs/Headers/Templates.apinotes b/clang/test/APINotes/Inputs/Headers/Templates.apinotes
index b7336484da0c7..ded688b5a9076 100644
--- a/clang/test/APINotes/Inputs/Headers/Templates.apinotes
+++ b/clang/test/APINotes/Inputs/Headers/Templates.apinotes
@@ -3,3 +3,5 @@ Name: Templates
Tags:
- Name: Box
SwiftImportAs: owned
+- Name: MoveOnly
+ SwiftCopyable: false
diff --git a/clang/test/APINotes/Inputs/Headers/Templates.h b/clang/test/APINotes/Inputs/Headers/Templates.h
index 2a86a46d4af27..eee2fec846e7c 100644
--- a/clang/test/APINotes/Inputs/Headers/Templates.h
+++ b/clang/test/APINotes/Inputs/Headers/Templates.h
@@ -8,3 +8,17 @@ struct Box {
using FloatBox = Box<float>;
using IntBox = Box<int>;
+
+template <typename T>
+struct MoveOnly {
+ T value;
+};
+
+template <>
+struct MoveOnly<float> {
+ double value;
+};
+
+struct MoveOnlyBox {
+ MoveOnly<int> value;
+};
diff --git a/clang/test/APINotes/templates.cpp b/clang/test/APINotes/templates.cpp
index 48109011e73a9..ef887196cabe7 100644
--- a/clang/test/APINotes/templates.cpp
+++ b/clang/test/APINotes/templates.cpp
@@ -1,6 +1,7 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache/Tmpl -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers -F %S/Inputs/Frameworks %s -x c++
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache/Tmpl -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers -F %S/Inputs/Frameworks %s -ast-dump -ast-dump-filter Box -x c++ | FileCheck -check-prefix=CHECK-BOX %s
+// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache/Tmpl -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers -F %S/Inputs/Frameworks %s -ast-dump -ast-dump-filter MoveOnly -x c++ | FileCheck -check-prefix=CHECK-MOVEONLY %s
#include "Templates.h"
@@ -10,3 +11,24 @@
// Make sure the attributes aren't duplicated.
// CHECK-BOX-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "import_owned"
+
+// CHECK-MOVEONLY: Dumping MoveOnly:
+// CHECK-MOVEONLY-NEXT: ClassTemplateDecl {{.+}} imported in Templates MoveOnly
+// CHECK-MOVEONLY: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
+
+// Make sure the attributes aren't duplicated.
+// CHECK-MOVEONLY-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
+
+// CHECK-MOVEONLY: ClassTemplateSpecializationDecl {{.+}} imported in Templates {{.+}} MoveOnly
+// CHECK-MOVEONLY: TemplateArgument type 'int'
+// CHECK-MOVEONLY: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
+
+// Make sure the attributes aren't duplicated.
+// CHECK-MOVEONLY-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
+
+// CHECK-MOVEONLY: ClassTemplateSpecializationDecl {{.+}} imported in Templates {{.+}} MoveOnly
+// CHECK-MOVEONLY: TemplateArgument type 'float'
+// CHECK-MOVEONLY: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
+
+// Make sure the attributes aren't duplicated.
+// CHECK-MOVEONLY-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
More information about the cfe-commits
mailing list