[clang] [clang] Make nullability-on-classes more robust to redeclarations (PR #114778)

2024-11-04 Thread Sam McCall via cfe-commits

https://github.com/sam-mccall closed 
https://github.com/llvm/llvm-project/pull/114778
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Make nullability-on-classes more robust to redeclarations (PR #114778)

2024-11-04 Thread Sam McCall via cfe-commits

https://github.com/sam-mccall updated 
https://github.com/llvm/llvm-project/pull/114778

>From 58baaccd4849fee2f8f1966de62660cb4c5bfa6d Mon Sep 17 00:00:00 2001
From: Sam McCall 
Date: Mon, 4 Nov 2024 12:48:35 +0100
Subject: [PATCH 1/2] [clang] Make nullability-on-classes more robust to
 redeclarations

This is relevant after b24650e814e55d90acfc40acf045456c98f32b9c where
the selected template decl can be anything, even apparently a friend
declaration in some cases.
---
 clang/lib/AST/Type.cpp| 18 ++-
 clang/test/SemaCXX/nullability_redecl.cpp | 27 +++
 2 files changed, 40 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/SemaCXX/nullability_redecl.cpp

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 229721aeae8114..a01d7be0cacc95 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -44,6 +44,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
@@ -4774,7 +4775,10 @@ bool Type::canHaveNullability(bool ResultIfUnknown) 
const {
 ->getTemplateName()
 .getAsTemplateDecl())
   if (auto *CTD = dyn_cast(templateDecl))
-return CTD->getTemplatedDecl()->hasAttr();
+return llvm::any_of(
+CTD->redecls(), [](const RedeclarableTemplateDecl *RTD) {
+  return RTD->getTemplatedDecl()->hasAttr();
+});
 return ResultIfUnknown;
 
   case Type::Builtin:
@@ -4841,10 +4845,14 @@ bool Type::canHaveNullability(bool ResultIfUnknown) 
const {
 // For template specializations, look only at primary template attributes.
 // This is a consistent regardless of whether the instantiation is known.
 if (const auto *CTSD = dyn_cast(RD))
-  return CTSD->getSpecializedTemplate()
-  ->getTemplatedDecl()
-  ->hasAttr();
-return RD->hasAttr();
+  return llvm::any_of(
+  CTSD->getSpecializedTemplate()->redecls(),
+  [](const RedeclarableTemplateDecl *RTD) {
+return RTD->getTemplatedDecl()->hasAttr();
+  });
+return llvm::any_of(RD->redecls(), [](const TagDecl *RD) {
+  return RD->hasAttr();
+});
   }
 
   // Non-pointer types.
diff --git a/clang/test/SemaCXX/nullability_redecl.cpp 
b/clang/test/SemaCXX/nullability_redecl.cpp
new file mode 100644
index 00..99bc521b89c13c
--- /dev/null
+++ b/clang/test/SemaCXX/nullability_redecl.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-nullability-declspec %s 
-verify -Wnullable-to-nonnull-conversion -I%S/Inputs
+
+class Foo;
+using Foo1 = Foo _Nonnull; // expected-error{{nullability specifier '_Nonnull' 
cannot be applied to non-pointer type 'Foo'}}
+class _Nullable Foo;
+using Foo2 = Foo _Nonnull;
+class Foo;
+using Foo3 = Foo _Nonnull;
+
+template 
+class Bar;
+using Bar1 = Bar _Nonnull; // expected-error{{nullability specifier 
'_Nonnull' cannot be applied to non-pointer type 'Bar'}}
+template 
+class _Nullable Bar;
+using Bar2 = Bar _Nonnull;
+template 
+class Bar;
+using Bar3 = Bar _Nonnull;
+
+namespace std {
+  template class unique_ptr;
+  using UP1 = unique_ptr _Nonnull;
+  class X { template friend class unique_ptr; };
+  using UP2 = unique_ptr _Nonnull;
+  template class unique_ptr;
+  using UP3 = unique_ptr _Nonnull;
+}

>From 4af29641598022fd263f995ae548941add3de3fd Mon Sep 17 00:00:00 2001
From: Sam McCall 
Date: Mon, 4 Nov 2024 13:03:27 +0100
Subject: [PATCH 2/2] Fix include order

---
 clang/lib/AST/Type.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index a01d7be0cacc95..6bf2908e667c07 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -43,8 +43,8 @@
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Make nullability-on-classes more robust to redeclarations (PR #114778)

2024-11-04 Thread Dmitri Gribenko via cfe-commits

https://github.com/gribozavr approved this pull request.


https://github.com/llvm/llvm-project/pull/114778
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Make nullability-on-classes more robust to redeclarations (PR #114778)

2024-11-04 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 5e75880165553e9afb721239689a9c79ec84a108 
58baaccd4849fee2f8f1966de62660cb4c5bfa6d --extensions cpp -- 
clang/test/SemaCXX/nullability_redecl.cpp clang/lib/AST/Type.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index a01d7be0ca..6bf2908e66 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -43,8 +43,8 @@
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"

``




https://github.com/llvm/llvm-project/pull/114778
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Make nullability-on-classes more robust to redeclarations (PR #114778)

2024-11-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Sam McCall (sam-mccall)


Changes

This is relevant after b24650e814e55d90acfc40acf045456c98f32b9c where
the selected template decl can be anything, even apparently a friend
declaration in some cases.


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


2 Files Affected:

- (modified) clang/lib/AST/Type.cpp (+13-5) 
- (added) clang/test/SemaCXX/nullability_redecl.cpp (+27) 


``diff
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 229721aeae8114..a01d7be0cacc95 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -44,6 +44,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
@@ -4774,7 +4775,10 @@ bool Type::canHaveNullability(bool ResultIfUnknown) 
const {
 ->getTemplateName()
 .getAsTemplateDecl())
   if (auto *CTD = dyn_cast(templateDecl))
-return CTD->getTemplatedDecl()->hasAttr();
+return llvm::any_of(
+CTD->redecls(), [](const RedeclarableTemplateDecl *RTD) {
+  return RTD->getTemplatedDecl()->hasAttr();
+});
 return ResultIfUnknown;
 
   case Type::Builtin:
@@ -4841,10 +4845,14 @@ bool Type::canHaveNullability(bool ResultIfUnknown) 
const {
 // For template specializations, look only at primary template attributes.
 // This is a consistent regardless of whether the instantiation is known.
 if (const auto *CTSD = dyn_cast(RD))
-  return CTSD->getSpecializedTemplate()
-  ->getTemplatedDecl()
-  ->hasAttr();
-return RD->hasAttr();
+  return llvm::any_of(
+  CTSD->getSpecializedTemplate()->redecls(),
+  [](const RedeclarableTemplateDecl *RTD) {
+return RTD->getTemplatedDecl()->hasAttr();
+  });
+return llvm::any_of(RD->redecls(), [](const TagDecl *RD) {
+  return RD->hasAttr();
+});
   }
 
   // Non-pointer types.
diff --git a/clang/test/SemaCXX/nullability_redecl.cpp 
b/clang/test/SemaCXX/nullability_redecl.cpp
new file mode 100644
index 00..99bc521b89c13c
--- /dev/null
+++ b/clang/test/SemaCXX/nullability_redecl.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-nullability-declspec %s 
-verify -Wnullable-to-nonnull-conversion -I%S/Inputs
+
+class Foo;
+using Foo1 = Foo _Nonnull; // expected-error{{nullability specifier '_Nonnull' 
cannot be applied to non-pointer type 'Foo'}}
+class _Nullable Foo;
+using Foo2 = Foo _Nonnull;
+class Foo;
+using Foo3 = Foo _Nonnull;
+
+template 
+class Bar;
+using Bar1 = Bar _Nonnull; // expected-error{{nullability specifier 
'_Nonnull' cannot be applied to non-pointer type 'Bar'}}
+template 
+class _Nullable Bar;
+using Bar2 = Bar _Nonnull;
+template 
+class Bar;
+using Bar3 = Bar _Nonnull;
+
+namespace std {
+  template class unique_ptr;
+  using UP1 = unique_ptr _Nonnull;
+  class X { template friend class unique_ptr; };
+  using UP2 = unique_ptr _Nonnull;
+  template class unique_ptr;
+  using UP3 = unique_ptr _Nonnull;
+}

``




https://github.com/llvm/llvm-project/pull/114778
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Make nullability-on-classes more robust to redeclarations (PR #114778)

2024-11-04 Thread Sam McCall via cfe-commits

https://github.com/sam-mccall created 
https://github.com/llvm/llvm-project/pull/114778

This is relevant after b24650e814e55d90acfc40acf045456c98f32b9c where
the selected template decl can be anything, even apparently a friend
declaration in some cases.


>From 58baaccd4849fee2f8f1966de62660cb4c5bfa6d Mon Sep 17 00:00:00 2001
From: Sam McCall 
Date: Mon, 4 Nov 2024 12:48:35 +0100
Subject: [PATCH] [clang] Make nullability-on-classes more robust to
 redeclarations

This is relevant after b24650e814e55d90acfc40acf045456c98f32b9c where
the selected template decl can be anything, even apparently a friend
declaration in some cases.
---
 clang/lib/AST/Type.cpp| 18 ++-
 clang/test/SemaCXX/nullability_redecl.cpp | 27 +++
 2 files changed, 40 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/SemaCXX/nullability_redecl.cpp

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 229721aeae8114..a01d7be0cacc95 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -44,6 +44,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
@@ -4774,7 +4775,10 @@ bool Type::canHaveNullability(bool ResultIfUnknown) 
const {
 ->getTemplateName()
 .getAsTemplateDecl())
   if (auto *CTD = dyn_cast(templateDecl))
-return CTD->getTemplatedDecl()->hasAttr();
+return llvm::any_of(
+CTD->redecls(), [](const RedeclarableTemplateDecl *RTD) {
+  return RTD->getTemplatedDecl()->hasAttr();
+});
 return ResultIfUnknown;
 
   case Type::Builtin:
@@ -4841,10 +4845,14 @@ bool Type::canHaveNullability(bool ResultIfUnknown) 
const {
 // For template specializations, look only at primary template attributes.
 // This is a consistent regardless of whether the instantiation is known.
 if (const auto *CTSD = dyn_cast(RD))
-  return CTSD->getSpecializedTemplate()
-  ->getTemplatedDecl()
-  ->hasAttr();
-return RD->hasAttr();
+  return llvm::any_of(
+  CTSD->getSpecializedTemplate()->redecls(),
+  [](const RedeclarableTemplateDecl *RTD) {
+return RTD->getTemplatedDecl()->hasAttr();
+  });
+return llvm::any_of(RD->redecls(), [](const TagDecl *RD) {
+  return RD->hasAttr();
+});
   }
 
   // Non-pointer types.
diff --git a/clang/test/SemaCXX/nullability_redecl.cpp 
b/clang/test/SemaCXX/nullability_redecl.cpp
new file mode 100644
index 00..99bc521b89c13c
--- /dev/null
+++ b/clang/test/SemaCXX/nullability_redecl.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-nullability-declspec %s 
-verify -Wnullable-to-nonnull-conversion -I%S/Inputs
+
+class Foo;
+using Foo1 = Foo _Nonnull; // expected-error{{nullability specifier '_Nonnull' 
cannot be applied to non-pointer type 'Foo'}}
+class _Nullable Foo;
+using Foo2 = Foo _Nonnull;
+class Foo;
+using Foo3 = Foo _Nonnull;
+
+template 
+class Bar;
+using Bar1 = Bar _Nonnull; // expected-error{{nullability specifier 
'_Nonnull' cannot be applied to non-pointer type 'Bar'}}
+template 
+class _Nullable Bar;
+using Bar2 = Bar _Nonnull;
+template 
+class Bar;
+using Bar3 = Bar _Nonnull;
+
+namespace std {
+  template class unique_ptr;
+  using UP1 = unique_ptr _Nonnull;
+  class X { template friend class unique_ptr; };
+  using UP2 = unique_ptr _Nonnull;
+  template class unique_ptr;
+  using UP3 = unique_ptr _Nonnull;
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits