r370417 - Refactor InitListChecker to make it a bit clearer that hasError is only

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 29 15:49:32 PDT 2019


Author: rsmith
Date: Thu Aug 29 15:49:32 2019
New Revision: 370417

URL: http://llvm.org/viewvc/llvm-project?rev=370417&view=rev
Log:
Refactor InitListChecker to make it a bit clearer that hasError is only
set to true in VerifyOnly mode in cases where it's also set to true when
actually building the initializer list.

Add FIXMEs for the two cases where that's not true. No functionality
change intended.

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=370417&r1=370416&r2=370417&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Thu Aug 29 15:49:32 2019
@@ -1093,49 +1093,32 @@ void InitListChecker::CheckExplicitInitL
   if (hadError)
     return;
 
-  if (Index < IList->getNumInits()) {
+  // Don't complain for incomplete types, since we'll get an error elsewhere.
+  if (Index < IList->getNumInits() && !T->isIncompleteType()) {
     // We have leftover initializers
+    bool ExtraInitsIsError = SemaRef.getLangOpts().CPlusPlus ||
+          (SemaRef.getLangOpts().OpenCL && T->isVectorType());
+    hadError = ExtraInitsIsError;
     if (VerifyOnly) {
-      if (SemaRef.getLangOpts().CPlusPlus ||
-          (SemaRef.getLangOpts().OpenCL &&
-           IList->getType()->isVectorType())) {
-        hadError = true;
-      }
       return;
-    }
-
-    if (StructuredIndex == 1 &&
-        IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) ==
-            SIF_None) {
-      unsigned DK = diag::ext_excess_initializers_in_char_array_initializer;
-      if (SemaRef.getLangOpts().CPlusPlus) {
-        DK = diag::err_excess_initializers_in_char_array_initializer;
-        hadError = true;
-      }
-      // Special-case
+    } else if (StructuredIndex == 1 &&
+               IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) ==
+                   SIF_None) {
+      unsigned DK =
+          ExtraInitsIsError
+              ? diag::err_excess_initializers_in_char_array_initializer
+              : diag::ext_excess_initializers_in_char_array_initializer;
       SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK)
           << IList->getInit(Index)->getSourceRange();
-    } else if (!T->isIncompleteType()) {
-      // Don't complain for incomplete types, since we'll get an error
-      // elsewhere
-      QualType CurrentObjectType = StructuredList->getType();
-      int initKind =
-        CurrentObjectType->isArrayType()? 0 :
-        CurrentObjectType->isVectorType()? 1 :
-        CurrentObjectType->isScalarType()? 2 :
-        CurrentObjectType->isUnionType()? 3 :
-        4;
-
-      unsigned DK = diag::ext_excess_initializers;
-      if (SemaRef.getLangOpts().CPlusPlus) {
-        DK = diag::err_excess_initializers;
-        hadError = true;
-      }
-      if (SemaRef.getLangOpts().OpenCL && initKind == 1) {
-        DK = diag::err_excess_initializers;
-        hadError = true;
-      }
+    } else {
+      int initKind = T->isArrayType() ? 0 :
+                     T->isVectorType() ? 1 :
+                     T->isScalarType() ? 2 :
+                     T->isUnionType() ? 3 :
+                     4;
 
+      unsigned DK = ExtraInitsIsError ? diag::err_excess_initializers
+                                      : diag::ext_excess_initializers;
       SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK)
           << initKind << IList->getInit(Index)->getSourceRange();
     }
@@ -1363,8 +1346,8 @@ void InitListChecker::CheckSubElementTyp
         hadError = true;
       else {
         ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get());
-          if (ExprRes.isInvalid())
-            hadError = true;
+        if (ExprRes.isInvalid())
+          hadError = true;
       }
       UpdateStructuredListElement(StructuredList, StructuredIndex,
                                   ExprRes.getAs<Expr>());
@@ -1389,10 +1372,15 @@ void InitListChecker::CheckSubElementTyp
     ++StructuredIndex;
   } else {
     if (!VerifyOnly) {
-      // We cannot initialize this element, so let
-      // PerformCopyInitialization produce the appropriate diagnostic.
-      SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr,
-                                        /*TopLevelOfInitList=*/true);
+      // We cannot initialize this element, so let PerformCopyInitialization
+      // produce the appropriate diagnostic. We already checked that this
+      // initialization will fail.
+      ExprResult Copy =
+          SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr,
+                                            /*TopLevelOfInitList=*/true);
+      (void)Copy;
+      assert(Copy.isInvalid() &&
+             "expected non-aggregate initialization to fail");
     }
     hadError = true;
     ++Index;
@@ -1872,6 +1860,8 @@ void InitListChecker::CheckArrayType(con
     // enough elements, or if we're performing an array new with an unknown
     // bound.
     // FIXME: This needs to detect holes left by designated initializers too.
+    // FIXME: Doing this now is wrong; these holes can be filled by later
+    // designated initializers.
     if ((maxElementsKnown && elementIndex < maxElements) ||
         Entity.isVariableLengthArrayNew())
       CheckEmptyInitializable(
@@ -2132,6 +2122,8 @@ void InitListChecker::CheckStructUnionTy
   if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() &&
       !Field->getType()->isIncompleteArrayType()) {
     // FIXME: Should check for holes left by designated initializers too.
+    // FIXME: Doing this now is wrong; these holes can be filled by later
+    // designated initializers.
     for (; Field != FieldEnd && !hadError; ++Field) {
       if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer())
         CheckEmptyInitializable(




More information about the cfe-commits mailing list