[clang] df2a6ee - [Sema] Use castAs<> instead getAs<> for dereferenced pointer casts. NFCI.

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 11 06:51:33 PST 2021


Author: Simon Pilgrim
Date: 2021-03-11T14:51:25Z
New Revision: df2a6ee3247c9d00b42c14ea924fd25cc45bda87

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

LOG: [Sema] Use castAs<> instead getAs<> for dereferenced pointer casts. NFCI.

getAs<> returns null for missed casts, resulting in null dereferences - use castAs<> instead which will assert the cast is correct.

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclAttr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index c309f0436437..459343637318 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2836,8 +2836,10 @@ static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
     QualType Ty = V->getType();
     if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
       const FunctionType *FT = Ty->isFunctionPointerType()
-       ? D->getFunctionType()
-       : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
+                                   ? D->getFunctionType()
+                                   : Ty->castAs<BlockPointerType>()
+                                         ->getPointeeType()
+                                         ->castAs<FunctionType>();
       if (!cast<FunctionProtoType>(FT)->isVariadic()) {
         int m = Ty->isFunctionPointerType() ? 0 : 1;
         S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
@@ -5836,7 +5838,7 @@ static void checkSwiftAsyncErrorBlock(Sema &S, Decl *D,
   // handleSwiftAsyncAttr already verified the type is correct, so no need to
   // double-check it here.
   const auto *FuncTy = HandlerParam->getType()
-                           ->getAs<BlockPointerType>()
+                           ->castAs<BlockPointerType>()
                            ->getPointeeType()
                            ->getAs<FunctionProtoType>();
   ArrayRef<QualType> BlockParams;
@@ -6313,8 +6315,8 @@ static void handleSwiftAsyncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
       return;
     }
     QualType BlockTy =
-        CompletionBlockType->getAs<BlockPointerType>()->getPointeeType();
-    if (!BlockTy->getAs<FunctionType>()->getReturnType()->isVoidType()) {
+        CompletionBlockType->castAs<BlockPointerType>()->getPointeeType();
+    if (!BlockTy->castAs<FunctionType>()->getReturnType()->isVoidType()) {
       S.Diag(CompletionBlock->getLocation(),
              diag::err_swift_async_bad_block_type)
           << CompletionBlock->getType();


        


More information about the cfe-commits mailing list