Author: Bill Wendling
Date: 2022-05-03T11:23:12-07:00
New Revision: f2639cf3fe46c30ad450cc2533c81eacd4788c33

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

LOG: [randstruct] Move initializer check to be more effective

If a randomized structure has an initializer with a dedicated
initializer in it, the field initialzed by that dedicated initializer
may end up at the end of the RecordDecl. This however may skip the
random layout initization check.

  struct t {
     int a, b, c, d, e;
  } x = { .a = 2, 4, 5, 6 };

Let's say that "a" is lands as the last field after randomization. The
call to CheckDesignatedInitializer sets the iterator to the end of the
initializer list. During the next iteration of the initializer list
check, it detects that and fails to issue the error about initializing
a randomized struct with non-designated initializer. Instead, it issues
an error about "excess elements in struct initializer", which is
confusing under these circumstances.

Reviewed By: aaron.ballman

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

Added: 
    

Modified: 
    clang/lib/Sema/SemaInit.cpp
    clang/test/Sema/init-randomized-struct.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 79c92bde3b770..016ff5ded25fb 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2170,11 +2170,6 @@ void InitListChecker::CheckStructUnionTypes(
       continue;
     }
 
-    if (Field == FieldEnd) {
-      // We've run out of fields. We're done.
-      break;
-    }
-
     // Check if this is an initializer of forms:
     //
     //   struct foo f = {};
@@ -2204,6 +2199,11 @@ void InitListChecker::CheckStructUnionTypes(
       break;
     }
 
+    if (Field == FieldEnd) {
+      // We've run out of fields. We're done.
+      break;
+    }
+
     // We've already initialized a member of a union. We're done.
     if (InitializedSomething && DeclType->isUnionType())
       break;

diff  --git a/clang/test/Sema/init-randomized-struct.c 
b/clang/test/Sema/init-randomized-struct.c
index a708d6a0cfd05..d421597fa522f 100644
--- a/clang/test/Sema/init-randomized-struct.c
+++ b/clang/test/Sema/init-randomized-struct.c
@@ -1,6 +1,13 @@
-// RUN: %clang_cc1 -triple=x86_64-unknown-linux 
-frandomize-layout-seed=1234567890abcdef \
+// RUN: %clang_cc1 -triple=x86_64-unknown-linux 
-frandomize-layout-seed=1234567890abcded \
 // RUN:  -verify -fsyntax-only -Werror %s
 
+// NOTE: The current seed (1234567890abcded) is specifically chosen because it
+// uncovered a bug in diagnostics. With it the randomization of "t9" places the
+// "a" element at the end of the record. When that happens, the clang complains
+// about excessive initializers, which is confusing, because there aren't
+// excessive initializers. It should instead complain about using a
+// non-designated initializer on a raqndomized struct.
+
 // Initializing a randomized structure requires a designated initializer,
 // otherwise the element ordering will be off. The only exceptions to this rule
 // are:


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to