[clang] fba8ad2 - [SemaTemplateInstantiate] Use cast<> instead of dyn_cast<> to avoid dereference of nullptr

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 11 03:29:57 PST 2022


Author: Simon Pilgrim
Date: 2022-01-11T11:29:38Z
New Revision: fba8ad2b719c14e971dfe16458b3d18c08e0e40d

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

LOG: [SemaTemplateInstantiate] Use cast<> instead of dyn_cast<> to avoid dereference of nullptr

The pointer is always dereferenced immediately below, so assert the cast is correct instead of returning nullptr

Added: 
    

Modified: 
    clang/lib/Sema/SemaTemplateInstantiate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 7d4c000e7e90..7c6bb4c8a5f8 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2790,11 +2790,10 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation,
     CurrentInstantiationScope = I->Scope;
 
     // Allow 'this' within late-parsed attributes.
-    NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl);
-    CXXRecordDecl *ThisContext =
-        dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
+    auto *ND = cast<NamedDecl>(I->NewDecl);
+    auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
     CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(),
-                               ND && ND->isCXXInstanceMember());
+                               ND->isCXXInstanceMember());
 
     Attr *NewAttr =
       instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);


        


More information about the cfe-commits mailing list