Skip to content

Conversation

@vvuksanovic
Copy link
Contributor

A flexible array member must remain the last field in the struct.

A flexible array member must remain the last field in the struct.
@llvmbot
Copy link
Member

llvmbot commented Sep 23, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Vladimir Vuksanovic (vvuksanovic)

Changes

A flexible array member must remain the last field in the struct.


Full diff: https://github.com/llvm/llvm-project/pull/160262.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp (+18-2)
  • (added) clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c (+10)
diff --git a/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp b/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
index affa276a0c550..5770bf767bc3c 100644
--- a/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
+++ b/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
@@ -164,6 +164,22 @@ getNewFieldsOrder(const RecordDecl *Definition,
   return NewFieldsOrder;
 }
 
+static bool isOrderValid(const RecordDecl *RD, ArrayRef<unsigned> FieldOrder) {
+  if (FieldOrder.empty())
+    return false;
+
+  // If there is a flexible array member in the struct, it must remain the last
+  // field.
+  if (RD->hasFlexibleArrayMember() &&
+      FieldOrder.back() != FieldOrder.size() - 1) {
+    llvm::errs()
+        << "Flexible array member must remain the last field in the struct\n";
+    return false;
+  }
+
+  return true;
+}
+
 struct ReorderedStruct {
 public:
   ReorderedStruct(const RecordDecl *Decl, ArrayRef<unsigned> NewFieldsOrder)
@@ -662,7 +678,7 @@ class ReorderingConsumer : public ASTConsumer {
       return;
     SmallVector<unsigned, 4> NewFieldsOrder =
         getNewFieldsOrder(RD, DesiredFieldsOrder);
-    if (NewFieldsOrder.empty())
+    if (!isOrderValid(RD, NewFieldsOrder))
       return;
     ReorderedStruct RS{RD, NewFieldsOrder};
 
@@ -699,7 +715,7 @@ class ReorderingConsumer : public ASTConsumer {
 
 std::unique_ptr<ASTConsumer> ReorderFieldsAction::newASTConsumer() {
   return std::make_unique<ReorderingConsumer>(RecordName, DesiredFieldsOrder,
-                                               Replacements);
+                                              Replacements);
 }
 
 } // namespace reorder_fields
diff --git a/clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c b/clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c
new file mode 100644
index 0000000000000..ef64350fd08e6
--- /dev/null
+++ b/clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c
@@ -0,0 +1,10 @@
+// RUN: clang-reorder-fields -record-name Foo -fields-order z,y,x %s -- 2>&1 | FileCheck --check-prefix=CHECK-BAD %s
+// RUN: clang-reorder-fields -record-name Foo -fields-order y,x,z %s -- | FileCheck --check-prefix=CHECK-GOOD %s
+
+// CHECK-BAD: {{^Flexible array member must remain the last field in the struct}}
+
+struct Foo {
+  int x;   // CHECK-GOOD:      {{^  int y;}}
+  int y;   // CHECK-GOOD-NEXT: {{^  int x;}}
+  int z[]; // CHECK-GOOD-NEXT: {{^  int z\[\];}}
+};

@vvuksanovic
Copy link
Contributor Author

@alexander-shaposhnikov

Copy link
Collaborator

@alexander-shaposhnikov alexander-shaposhnikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg

@vvuksanovic
Copy link
Contributor Author

Thanks, can you merge?

@alexander-shaposhnikov alexander-shaposhnikov merged commit 63e4550 into llvm:main Sep 29, 2025
11 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
A flexible array member must remain the last field in the struct.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants