[clang] 2bd8aee - [Clang] Fix unconditional access to Attr pointer when checking if _Nullable is applicable to a type

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 30 13:28:36 PST 2023


Author: Shafik Yaghmour
Date: 2023-01-30T13:28:29-08:00
New Revision: 2bd8aeea7e7d885fbf6c70c0e181b1e8e6b808a5

URL: https://github.com/llvm/llvm-project/commit/2bd8aeea7e7d885fbf6c70c0e181b1e8e6b808a5
DIFF: https://github.com/llvm/llvm-project/commit/2bd8aeea7e7d885fbf6c70c0e181b1e8e6b808a5.diff

LOG: [Clang] Fix unconditional access to Attr pointer when checking if _Nullable is applicable to a type

In TransformAttributedType(...) when checking if _Nullable can be applied to a
type it dereferences TL.getAttr() unconditionally which we can see from the code
earlier in the function is not correct since it is expected to be nullptr in
some cases.

It looks like the correct course of action is to use TL.getModifiedLoc() over
TL.getAttr()->getLocation() in the case that TL.getAttr() returns a nullptr.

Fixes: https://github.com/llvm/llvm-project/issues/60344

Differential Revision: https://reviews.llvm.org/D142799

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/TreeTransform.h
    clang/test/SemaCXX/nullability.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4c72ec64f6e19..94b466122a348 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -57,6 +57,9 @@ Bug Fixes
 - Fix crash on invalid code when looking up a destructor in a templated class
   inside a namespace. This fixes
   `Issue 59446 <https://github.com/llvm/llvm-project/issues/59446>`_.
+- Fix crash when diagnosing incorrect usage of ``_Nullable`` involving alias
+  templates. This fixes
+  `Issue 60344 <https://github.com/llvm/llvm-project/issues/60344>`_.
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 6a05ecc5370f8..ef56f6219868c 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6992,7 +6992,8 @@ QualType TreeTransform<Derived>::TransformAttributedType(
     // type sugar, and therefore cannot be diagnosed in any other way.
     if (auto nullability = oldType->getImmediateNullability()) {
       if (!modifiedType->canHaveNullability()) {
-        SemaRef.Diag(TL.getAttr()->getLocation(),
+        SemaRef.Diag((TL.getAttr() ? TL.getAttr()->getLocation()
+                                   : TL.getModifiedLoc().getBeginLoc()),
                      diag::err_nullability_nonpointer)
             << DiagNullabilityKind(*nullability, false) << modifiedType;
         return QualType();

diff  --git a/clang/test/SemaCXX/nullability.cpp b/clang/test/SemaCXX/nullability.cpp
index 978b90564a2a8..8d0c4dc195a6b 100644
--- a/clang/test/SemaCXX/nullability.cpp
+++ b/clang/test/SemaCXX/nullability.cpp
@@ -136,3 +136,9 @@ void arraysInLambdas() {
 void testNullabilityCompletenessWithTemplate() {
   Template<int*> tip;
 }
+
+namespace GH60344 {
+class a;
+template <typename b> using c = b _Nullable; // expected-error {{'_Nullable' cannot be applied to non-pointer type 'GH60344::a'}}
+c<a>;  // expected-note {{in instantiation of template type alias 'c' requested here}}
+}


        


More information about the cfe-commits mailing list