[clang-tools-extra] [clang-reorder-fields] Support designated initializers (PR #142150)

Clement Courbet via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 26 00:34:02 PDT 2025


================
@@ -0,0 +1,256 @@
+//===-- tools/extra/clang-reorder-fields/utils/Designator.cpp ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the definition of the DesignatorIter and Designators
+/// utility classes.
+///
+//===----------------------------------------------------------------------===//
+
+#include "Designator.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Expr.h"
+
+namespace clang {
+namespace reorder_fields {
+
+DesignatorIter::DesignatorIter(const QualType Type,
+                               RecordDecl::field_iterator Field,
+                               const RecordDecl *RD)
+    : Tag(STRUCT), Type(Type), StructIt({Field, RD}) {}
+
+DesignatorIter::DesignatorIter(const QualType Type, uint64_t Idx, uint64_t Size)
+    : Tag(ARRAY), Type(Type), ArrayIt({Idx, Size}) {}
+
+DesignatorIter::DesignatorIter(const QualType Type, uint64_t Start,
+                               uint64_t End, uint64_t Size)
+    : Tag(ARRAY_RANGE), Type(Type), ArrayRangeIt({Start, End, Size}) {}
+
+DesignatorIter &DesignatorIter::operator++() {
+  assert(!isFinished() && "Iterator is already finished");
+  switch (Tag) {
+  case STRUCT:
+    if (StructIt.Record->isUnion()) {
+      // Union always finishes on first increment.
+      StructIt.Field = StructIt.Record->field_end();
+      Type = QualType();
+      break;
+    }
+    ++StructIt.Field;
+    if (StructIt.Field != StructIt.Record->field_end()) {
+      Type = StructIt.Field->getType();
+    } else {
+      Type = QualType();
+    }
+    break;
+  case ARRAY:
+    ++ArrayIt.Index;
+    break;
+  case ARRAY_RANGE:
+    ArrayIt.Index = ArrayRangeIt.End + 1;
+    ArrayIt.Size = ArrayRangeIt.Size;
+    Tag = ARRAY;
+    break;
+  }
+  return *this;
+}
+
+bool DesignatorIter::isFinished() {
+  switch (Tag) {
+  case STRUCT:
+    return StructIt.Field == StructIt.Record->field_end();
+  case ARRAY:
+    return ArrayIt.Index == ArrayIt.Size;
+  case ARRAY_RANGE:
+    return ArrayRangeIt.End == ArrayRangeIt.Size;
+  }
+  return false;
+}
+
+DesignatorIter::Kind DesignatorIter::getTag() const { return Tag; }
+
+QualType DesignatorIter::getType() const { return Type; }
+
+RecordDecl::field_iterator &DesignatorIter::getStructIter() {
----------------
legrosbuffle wrote:

Inline all those ?

https://github.com/llvm/llvm-project/pull/142150


More information about the cfe-commits mailing list