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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 04:10:19 PST 2024


Author: Sam McCall
Date: 2024-11-04T13:10:15+01:00
New Revision: 17d8ed717fced72ed313ee7553309345630b0097

URL: https://github.com/llvm/llvm-project/commit/17d8ed717fced72ed313ee7553309345630b0097
DIFF: https://github.com/llvm/llvm-project/commit/17d8ed717fced72ed313ee7553309345630b0097.diff

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

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

Added: 
    clang/test/SemaCXX/nullability_redecl.cpp

Modified: 
    clang/lib/AST/Type.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 229721aeae8114..6bf2908e667c07 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -43,6 +43,7 @@
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -4774,7 +4775,10 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
                 ->getTemplateName()
                 .getAsTemplateDecl())
       if (auto *CTD = dyn_cast<ClassTemplateDecl>(templateDecl))
-        return CTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
+        return llvm::any_of(
+            CTD->redecls(), [](const RedeclarableTemplateDecl *RTD) {
+              return RTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
+            });
     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<ClassTemplateSpecializationDecl>(RD))
-      return CTSD->getSpecializedTemplate()
-          ->getTemplatedDecl()
-          ->hasAttr<TypeNullableAttr>();
-    return RD->hasAttr<TypeNullableAttr>();
+      return llvm::any_of(
+          CTSD->getSpecializedTemplate()->redecls(),
+          [](const RedeclarableTemplateDecl *RTD) {
+            return RTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
+          });
+    return llvm::any_of(RD->redecls(), [](const TagDecl *RD) {
+      return RD->hasAttr<TypeNullableAttr>();
+    });
   }
 
   // Non-pointer types.

diff  --git a/clang/test/SemaCXX/nullability_redecl.cpp b/clang/test/SemaCXX/nullability_redecl.cpp
new file mode 100644
index 00000000000000..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 T>
+class Bar;
+using Bar1 = Bar<int> _Nonnull; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'Bar<int>'}}
+template <class T>
+class _Nullable Bar;
+using Bar2 = Bar<int> _Nonnull;
+template <class T>
+class Bar;
+using Bar3 = Bar<int> _Nonnull;
+
+namespace std {
+  template<class T> class unique_ptr;
+  using UP1 = unique_ptr<int> _Nonnull;
+  class X { template<class T> friend class unique_ptr; };
+  using UP2 = unique_ptr<int> _Nonnull;
+  template<class T> class unique_ptr;
+  using UP3 = unique_ptr<int> _Nonnull;
+}


        


More information about the cfe-commits mailing list