[clang] Add Variadic 'dropAttrs' (PR #78476)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 09:11:10 PST 2024


https://github.com/erichkeane updated https://github.com/llvm/llvm-project/pull/78476

>From 6790e56b001a29e8bba012514eb3b12cd486d505 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Wed, 17 Jan 2024 09:03:14 -0800
Subject: [PATCH 1/2] Add Variadic 'dropAttrs'

As suggested in https://github.com/llvm/llvm-project/pull/78200

This adds a variadic 'dropAttrs', which drops all attributes of any of
the types specified.
---
 clang/include/clang/AST/DeclBase.h | 11 ++++++++---
 clang/lib/Sema/SemaDecl.cpp        |  3 +--
 clang/lib/Sema/SemaDeclCXX.cpp     |  3 +--
 clang/lib/Sema/SemaTemplate.cpp    |  6 ++----
 4 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 5b1038582bc674..933249b4a1a05e 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -548,17 +548,22 @@ class alignas(8) Decl {
     return hasAttrs() ? getAttrs().end() : nullptr;
   }
 
-  template <typename T>
-  void dropAttr() {
+  template<typename ...Ts>
+  void dropAttrs() {
     if (!HasAttrs) return;
 
     AttrVec &Vec = getAttrs();
-    llvm::erase_if(Vec, [](Attr *A) { return isa<T>(A); });
+    llvm::erase_if(Vec, [](Attr *A) { return isa<Ts...>(A); });
 
     if (Vec.empty())
       HasAttrs = false;
   }
 
+  template <typename T>
+  void dropAttr() {
+    dropAttrs<T>();
+  }
+
   template <typename T>
   llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
     return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>());
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index dae98f3a7406e8..5472b43aafd4f3 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7068,8 +7068,7 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
   if (WeakRefAttr *Attr = ND.getAttr<WeakRefAttr>()) {
     if (ND.isExternallyVisible()) {
       S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static);
-      ND.dropAttr<WeakRefAttr>();
-      ND.dropAttr<AliasAttr>();
+      ND.dropAttrs<WeakRefAttr, AliasAttr>();
     }
   }
 
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index a2ce96188b4f16..62dc623d5af378 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -6545,8 +6545,7 @@ void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) {
   if ((Context.getTargetInfo().getCXXABI().isMicrosoft() ||
        Context.getTargetInfo().getTriple().isPS()) &&
       (!Class->isExternallyVisible() && Class->hasExternalFormalLinkage())) {
-    Class->dropAttr<DLLExportAttr>();
-    Class->dropAttr<DLLImportAttr>();
+    Class->dropAttrs<DLLExportAttr, DLLImportAttr>();
     return;
   }
 
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index c0dcbda1fd6221..b5be596b7dff32 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -9229,10 +9229,8 @@ void Sema::CheckConceptRedefinition(ConceptDecl *NewDecl,
 /// that has just been explicitly specialized.
 static void StripImplicitInstantiation(NamedDecl *D, bool MinGW) {
   if (MinGW || (isa<FunctionDecl>(D) &&
-                cast<FunctionDecl>(D)->isFunctionTemplateSpecialization())) {
-    D->dropAttr<DLLImportAttr>();
-    D->dropAttr<DLLExportAttr>();
-  }
+                cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()))
+    D->dropAttrs< DLLImportAttr, DLLExportAttr>();
 
   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
     FD->setInlineSpecified(false);

>From 52053f3b2cf01ddc1706e84e6e1fd9fdc3c9e966 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Wed, 17 Jan 2024 09:11:00 -0800
Subject: [PATCH 2/2] fix clang-format

---
 clang/include/clang/AST/DeclBase.h | 8 ++------
 clang/lib/Sema/SemaTemplate.cpp    | 2 +-
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 933249b4a1a05e..d957ea24f6394a 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -548,8 +548,7 @@ class alignas(8) Decl {
     return hasAttrs() ? getAttrs().end() : nullptr;
   }
 
-  template<typename ...Ts>
-  void dropAttrs() {
+  template <typename... Ts> void dropAttrs() {
     if (!HasAttrs) return;
 
     AttrVec &Vec = getAttrs();
@@ -559,10 +558,7 @@ class alignas(8) Decl {
       HasAttrs = false;
   }
 
-  template <typename T>
-  void dropAttr() {
-    dropAttrs<T>();
-  }
+  template <typename T> void dropAttr() { dropAttrs<T>(); }
 
   template <typename T>
   llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index b5be596b7dff32..0655d363352067 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -9230,7 +9230,7 @@ void Sema::CheckConceptRedefinition(ConceptDecl *NewDecl,
 static void StripImplicitInstantiation(NamedDecl *D, bool MinGW) {
   if (MinGW || (isa<FunctionDecl>(D) &&
                 cast<FunctionDecl>(D)->isFunctionTemplateSpecialization()))
-    D->dropAttrs< DLLImportAttr, DLLExportAttr>();
+    D->dropAttrs<DLLImportAttr, DLLExportAttr>();
 
   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
     FD->setInlineSpecified(false);



More information about the cfe-commits mailing list