r373474 - SemaInit - silence static analyzer getAs<> null dereference warnings. NFCI.

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 2 07:02:19 PDT 2019


Author: rksimon
Date: Wed Oct  2 07:02:18 2019
New Revision: 373474

URL: http://llvm.org/viewvc/llvm-project?rev=373474&view=rev
Log:
SemaInit - silence static analyzer getAs<> null dereference warnings. NFCI.

The static analyzer is warning about potential null dereferences, but in these cases we should be able to use castAs<> directly and if not assert will fire for us.

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=373474&r1=373473&r2=373474&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Wed Oct  2 07:02:18 2019
@@ -974,7 +974,7 @@ int InitListChecker::numArrayElements(Qu
 }
 
 int InitListChecker::numStructUnionElements(QualType DeclType) {
-  RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
+  RecordDecl *structDecl = DeclType->castAs<RecordType>()->getDecl();
   int InitializableMembers = 0;
   if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl))
     InitializableMembers += CXXRD->getNumBases();
@@ -1033,7 +1033,7 @@ void InitListChecker::CheckImplicitInitL
   else if (T->isRecordType())
     maxElements = numStructUnionElements(T);
   else if (T->isVectorType())
-    maxElements = T->getAs<VectorType>()->getNumElements();
+    maxElements = T->castAs<VectorType>()->getNumElements();
   else
     llvm_unreachable("CheckImplicitInitList(): Illegal type");
 
@@ -1264,7 +1264,7 @@ void InitListChecker::CheckListElementTy
   } else if (DeclType->isRecordType()) {
     assert(DeclType->isAggregateType() &&
            "non-aggregate records should be handed in CheckSubElementType");
-    RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
+    RecordDecl *RD = DeclType->castAs<RecordType>()->getDecl();
     auto Bases =
         CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(),
                                         CXXRecordDecl::base_class_iterator());
@@ -1490,7 +1490,7 @@ void InitListChecker::CheckComplexType(c
         << IList->getSourceRange();
 
   // Initialize the complex number.
-  QualType elementType = DeclType->getAs<ComplexType>()->getElementType();
+  QualType elementType = DeclType->castAs<ComplexType>()->getElementType();
   InitializedEntity ElementEntity =
     InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
 
@@ -1636,7 +1636,7 @@ void InitListChecker::CheckVectorType(co
                                       unsigned &Index,
                                       InitListExpr *StructuredList,
                                       unsigned &StructuredIndex) {
-  const VectorType *VT = DeclType->getAs<VectorType>();
+  const VectorType *VT = DeclType->castAs<VectorType>();
   unsigned maxElements = VT->getNumElements();
   unsigned numEltsInit = 0;
   QualType elementType = VT->getElementType();
@@ -1706,7 +1706,7 @@ void InitListChecker::CheckVectorType(co
       return;
 
     bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian();
-    const VectorType *T = Entity.getType()->getAs<VectorType>();
+    const VectorType *T = Entity.getType()->castAs<VectorType>();
     if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector ||
                         T->getVectorKind() == VectorType::NeonPolyVector)) {
       // The ability to use vector initializer lists is a GNU vector extension
@@ -1762,7 +1762,7 @@ void InitListChecker::CheckVectorType(co
       ++numEltsInit;
     } else {
       QualType VecType;
-      const VectorType *IVT = IType->getAs<VectorType>();
+      const VectorType *IVT = IType->castAs<VectorType>();
       unsigned numIElts = IVT->getNumElements();
 
       if (IType->isExtVectorType())
@@ -1995,7 +1995,7 @@ void InitListChecker::CheckStructUnionTy
     bool SubobjectIsDesignatorContext, unsigned &Index,
     InitListExpr *StructuredList, unsigned &StructuredIndex,
     bool TopLevelObject) {
-  RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
+  RecordDecl *structDecl = DeclType->castAs<RecordType>()->getDecl();
 
   // If the record is invalid, some of it's members are invalid. To avoid
   // confusion, we forgo checking the intializer for the entire record.
@@ -2007,7 +2007,7 @@ void InitListChecker::CheckStructUnionTy
   }
 
   if (DeclType->isUnionType() && IList->getNumInits() == 0) {
-    RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
+    RecordDecl *RD = DeclType->castAs<RecordType>()->getDecl();
 
     if (!VerifyOnly)
       for (FieldDecl *FD : RD->fields()) {
@@ -2082,7 +2082,7 @@ void InitListChecker::CheckStructUnionTy
   // anything except look at designated initializers; That's okay,
   // because an error should get printed out elsewhere. It might be
   // worthwhile to skip over the rest of the initializer, though.
-  RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
+  RecordDecl *RD = DeclType->castAs<RecordType>()->getDecl();
   RecordDecl::field_iterator FieldEnd = RD->field_end();
   bool CheckForMissingFields =
     !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts());
@@ -4206,7 +4206,7 @@ static void TryReferenceListInitializati
   }
 
   QualType DestType = Entity.getType();
-  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
+  QualType cv1T1 = DestType->castAs<ReferenceType>()->getPointeeType();
   Qualifiers T1Quals;
   QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
 
@@ -4463,7 +4463,7 @@ static OverloadingResult TryRefInitWithC
     Expr *Initializer, bool AllowRValues, bool IsLValueRef,
     InitializationSequence &Sequence) {
   QualType DestType = Entity.getType();
-  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
+  QualType cv1T1 = DestType->castAs<ReferenceType>()->getPointeeType();
   QualType T1 = cv1T1.getUnqualifiedType();
   QualType cv2T2 = Initializer->getType();
   QualType T2 = cv2T2.getUnqualifiedType();
@@ -4656,7 +4656,7 @@ static void TryReferenceInitialization(S
                                        Expr *Initializer,
                                        InitializationSequence &Sequence) {
   QualType DestType = Entity.getType();
-  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
+  QualType cv1T1 = DestType->castAs<ReferenceType>()->getPointeeType();
   Qualifiers T1Quals;
   QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
   QualType cv2T2 = Initializer->getType();
@@ -8201,7 +8201,7 @@ ExprResult InitializationSequence::Perfo
           Ty = S.Context.getRValueReferenceType(Ty);
         else if ((*ResultType)->isLValueReferenceType())
           Ty = S.Context.getLValueReferenceType(Ty,
-            (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue());
+            (*ResultType)->castAs<LValueReferenceType>()->isSpelledAsLValue());
         *ResultType = Ty;
       }
 
@@ -8659,7 +8659,7 @@ static void diagnoseListInit(Sema &S, co
     // A list-initialization failure for a reference means that we tried to
     // create a temporary of the inner type (per [dcl.init.list]p3.6) and the
     // inner initialization failed.
-    QualType T = DestType->getAs<ReferenceType>()->getPointeeType();
+    QualType T = DestType->castAs<ReferenceType>()->getPointeeType();
     diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList);
     SourceLocation Loc = InitList->getBeginLoc();
     if (auto *D = Entity.getDecl())
@@ -9016,7 +9016,7 @@ bool InitializationSequence::Diagnose(Se
               << InheritedFrom;
 
             RecordDecl *BaseDecl
-              = Entity.getBaseSpecifier()->getType()->getAs<RecordType>()
+              = Entity.getBaseSpecifier()->getType()->castAs<RecordType>()
                                                                   ->getDecl();
             S.Diag(BaseDecl->getLocation(), diag::note_previous_decl)
               << S.Context.getTagDeclType(BaseDecl);




More information about the cfe-commits mailing list