[clang] [APINotes] Avoid duplicate attributes when fields instantiate class templates (PR #173386)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 23 07:12:10 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Susana Monteiro (susmonteiro)
<details>
<summary>Changes</summary>
If a C++ class template `A` is annotated via API Notes and another class `B` has a field of type `A`, we would apply the attributes from the API Notes twice. This happened during `ActOnFields`, so this change makes sure we stop processing API Notes for class template instantiations in this function.
rdar://166179307
---
Full diff: https://github.com/llvm/llvm-project/pull/173386.diff
4 Files Affected:
- (modified) clang/lib/Sema/SemaDecl.cpp (+2-1)
- (modified) clang/test/APINotes/Inputs/Headers/Templates.apinotes (+2)
- (modified) clang/test/APINotes/Inputs/Headers/Templates.h (+9)
- (modified) clang/test/APINotes/templates.cpp (+11)
``````````diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5da665a5acad2..c44f4bd6b331a 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 (!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..cdb7c3b3fa2c0 100644
--- a/clang/test/APINotes/Inputs/Headers/Templates.h
+++ b/clang/test/APINotes/Inputs/Headers/Templates.h
@@ -8,3 +8,12 @@ struct Box {
using FloatBox = Box<float>;
using IntBox = Box<int>;
+
+template <typename T>
+struct MoveOnly {
+ T value;
+};
+
+struct MoveOnlyBox {
+ MoveOnly<int> value;
+};
diff --git a/clang/test/APINotes/templates.cpp b/clang/test/APINotes/templates.cpp
index 48109011e73a9..6d8b65a0d5f85 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,13 @@
// 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"
+
+// CHECK-MOVEONLY-NEXT: ClassTemplateSpecializationDecl {{.+}} imported in Templates {{.+}} MoveOnly
+// CHECK-MOVEONLY: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
+
+// Make sure the attributes aren't duplicated.
+// CHECK-MOVEONLY-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
``````````
</details>
https://github.com/llvm/llvm-project/pull/173386
More information about the cfe-commits
mailing list