stevewan updated this revision to Diff 368716.
stevewan marked an inline comment as done.
stevewan added a comment.

Avoid unannotated fall-through between cases


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D107394/new/

https://reviews.llvm.org/D107394

Files:
  clang/lib/AST/RecordLayoutBuilder.cpp
  clang/test/Layout/aix-alignof-align-and-pack-attr.cpp
  clang/test/Layout/aix-power-alignment-typedef.cpp
  clang/test/Layout/aix-type-align-and-pack-attr.cpp

Index: clang/test/Layout/aix-type-align-and-pack-attr.cpp
===================================================================
--- /dev/null
+++ clang/test/Layout/aix-type-align-and-pack-attr.cpp
@@ -0,0 +1,59 @@
+// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \
+// RUN:   FileCheck %s
+
+// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \
+// RUN:   FileCheck %s
+
+namespace test1 {
+struct __attribute__((__aligned__(2))) S {
+  double d;
+};
+
+S s;
+
+// CHECK: @{{.*}}test1{{.*}}s{{.*}} = global %"struct.test1::S" zeroinitializer, align 8
+} // namespace test1
+
+namespace test2 {
+struct __attribute__((__aligned__(2), packed)) S {
+  double d;
+};
+
+S s;
+
+// CHECK: @{{.*}}test2{{.*}}s{{.*}} = global %"struct.test2::S" zeroinitializer, align 2
+} // namespace test2
+
+namespace test3 {
+struct __attribute__((__aligned__(16))) S {
+  double d;
+};
+
+S s;
+
+// CHECK: @{{.*}}test3{{.*}}s{{.*}} = global %"struct.test3::S" zeroinitializer, align 16
+} // namespace test3
+
+namespace test4 {
+struct __attribute__((aligned(2))) SS {
+  double d;
+};
+
+struct S {
+  struct SS ss;
+} s;
+
+// CHECK: @{{.*}}test4{{.*}}s{{.*}} = global %"struct.test4::S" zeroinitializer, align 8
+} // namespace test4
+
+namespace test5 {
+struct __attribute__((aligned(2), packed)) SS {
+  double d;
+};
+
+struct S {
+  struct SS ss;
+} s;
+
+// CHECK: @{{.*}}test5{{.*}}s{{.*}} = global %"struct.test5::S" zeroinitializer, align 2
+} // namespace test5
Index: clang/test/Layout/aix-power-alignment-typedef.cpp
===================================================================
--- clang/test/Layout/aix-power-alignment-typedef.cpp
+++ clang/test/Layout/aix-power-alignment-typedef.cpp
@@ -37,3 +37,39 @@
 // CHECK-NEXT:       |  nvsize=2, nvalign=2, preferrednvalign=2]
 
 } // namespace test2
+
+namespace test3 {
+typedef double DblArr[] __attribute__((__aligned__(2)));
+
+union U {
+  DblArr da;
+  char x;
+};
+
+int x = sizeof(U);
+
+// CHECK:          0 | union test3::U
+// CHECK-NEXT:     0 |   test3::DblArr da
+// CHECK-NEXT:     0 |   char x
+// CHECK-NEXT:       | [sizeof=2, dsize=2, align=2, preferredalign=2,
+// CHECK-NEXT:       |  nvsize=2, nvalign=2, preferrednvalign=2]
+
+} // namespace test3
+
+namespace test4 {
+typedef double Dbl __attribute__((__aligned__(2)));
+
+union U {
+  Dbl DblArr[];
+  char x;
+};
+
+int x = sizeof(U);
+
+// CHECK:          0 | union test4::U
+// CHECK-NEXT:     0 |   test4::Dbl [] DblArr
+// CHECK-NEXT:     0 |   char x
+// CHECK-NEXT:       | [sizeof=2, dsize=2, align=2, preferredalign=2,
+// CHECK-NEXT:       |  nvsize=2, nvalign=2, preferrednvalign=2]
+
+} // namespace test4
Index: clang/test/Layout/aix-alignof-align-and-pack-attr.cpp
===================================================================
--- clang/test/Layout/aix-alignof-align-and-pack-attr.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \
-// RUN:   FileCheck %s
-
-// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \
-// RUN:   FileCheck %s
-
-namespace test1 {
-struct __attribute__((__aligned__(2))) C {
-  double x;
-} c;
-
-// CHECK: @{{.*}}test1{{.*}}c{{.*}} = global %"struct.test1::C" zeroinitializer, align 8
-} // namespace test1
-
-namespace test2 {
-struct __attribute__((__aligned__(2), packed)) C {
-  double x;
-} c;
-
-// CHECK: @{{.*}}test2{{.*}}c{{.*}} = global %"struct.test2::C" zeroinitializer, align 2
-} // namespace test2
-
-namespace test3 {
-struct __attribute__((__aligned__(16))) C {
-  double x;
-} c;
-
-// CHECK: @{{.*}}test3{{.*}}c{{.*}} = global %"struct.test3::C" zeroinitializer, align 16
-} // namespace test3
Index: clang/lib/AST/RecordLayoutBuilder.cpp
===================================================================
--- clang/lib/AST/RecordLayoutBuilder.cpp
+++ clang/lib/AST/RecordLayoutBuilder.cpp
@@ -1978,8 +1978,144 @@
   // and zero-width bit-fields count as prior members; members of empty class
   // types marked `no_unique_address` are not considered to be prior members.
   CharUnits PreferredAlign = FieldAlign;
-  if (DefaultsToAIXPowerAlignment && !AlignIsRequired &&
+  if (DefaultsToAIXPowerAlignment &&
       (FoundFirstNonOverlappingEmptyFieldForAIX || IsNaturalAlign)) {
+    auto checkTypeIsTypedef = [](auto &&checkTypeIsTypedef,
+                                 const auto Ty) -> bool {
+      bool isTypedef = false;
+      switch (Ty->getTypeClass()) {
+      case Type::Typedef: {
+        isTypedef = true;
+        break;
+      }
+      // Recursively check if a type can be unwound to a typedef.
+#define TYPE(Class, Base)
+#define ABSTRACT_TYPE(Class, Base)
+#define NON_CANONICAL_TYPE(Class, Base)
+#define DEPENDENT_TYPE(Class, Base) case Type::Class:
+#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)                       \
+  case Type::Class:                                                            \
+    assert(!Ty->isDependentType() && "should not see dependent types here");   \
+    return checkTypeIsTypedef(checkTypeIsTypedef,                              \
+                              cast<Class##Type>(Ty)->desugar().getTypePtr());
+#include "clang/AST/TypeNodes.inc"
+        llvm_unreachable("Should not see dependent types");
+      case Type::IncompleteArray:
+      case Type::VariableArray:
+      case Type::ConstantArray: {
+        isTypedef = checkTypeIsTypedef(checkTypeIsTypedef,
+                                       cast<ArrayType>(Ty)->getElementType());
+        break;
+      }
+      case Type::ExtVector:
+      case Type::Vector: {
+        isTypedef = checkTypeIsTypedef(checkTypeIsTypedef,
+                                       cast<VectorType>(Ty)->getElementType());
+        break;
+      }
+      case Type::ConstantMatrix: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef, cast<ConstantMatrixType>(Ty)->getElementType());
+        break;
+      }
+      case Type::Complex: {
+        isTypedef = checkTypeIsTypedef(checkTypeIsTypedef,
+                                       cast<ComplexType>(Ty)->getElementType());
+        break;
+      }
+      case Type::ObjCObject: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr());
+        break;
+      }
+      case Type::Adjusted:
+      case Type::Decayed: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<AdjustedType>(Ty)->getAdjustedType().getTypePtr());
+        break;
+      }
+      case Type::Enum: {
+        const auto *TT = cast<TagType>(Ty);
+
+        if (TT->getDecl()->isInvalidDecl())
+          break;
+        if (const auto *ET = dyn_cast<EnumType>(TT)) {
+          isTypedef = checkTypeIsTypedef(
+              checkTypeIsTypedef,
+              ET->getDecl()->getIntegerType()->getUnqualifiedDesugaredType());
+        }
+        break;
+      }
+      case Type::SubstTemplateTypeParm: {
+        isTypedef = checkTypeIsTypedef(checkTypeIsTypedef,
+                                       cast<SubstTemplateTypeParmType>(Ty)
+                                           ->getReplacementType()
+                                           .getTypePtr());
+        break;
+      }
+      case Type::Auto:
+      case Type::DeducedTemplateSpecialization: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<DeducedType>(Ty)->getDeducedType().getTypePtr());
+        break;
+      }
+      case Type::Paren: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<ParenType>(Ty)->getInnerType().getTypePtr());
+        break;
+      }
+      case Type::MacroQualified: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<MacroQualifiedType>(Ty)->getUnderlyingType().getTypePtr());
+        break;
+      }
+      case Type::ObjCTypeParam: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<ObjCTypeParamType>(Ty)->desugar().getTypePtr());
+        break;
+      }
+      case Type::Elaborated: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<ElaboratedType>(Ty)->getNamedType().getTypePtr());
+        break;
+      }
+      case Type::Attributed: {
+        isTypedef = checkTypeIsTypedef(
+            checkTypeIsTypedef,
+            cast<AttributedType>(Ty)->getEquivalentType().getTypePtr());
+        break;
+      }
+      case Type::Atomic: {
+        isTypedef = checkTypeIsTypedef(checkTypeIsTypedef,
+                                       cast<AtomicType>(Ty)->getValueType());
+        break;
+      }
+      // Account for the rest of the types.
+      case Type::FunctionNoProto:
+      case Type::FunctionProto:
+      case Type::Builtin:
+      case Type::ObjCObjectPointer:
+      case Type::BlockPointer:
+      case Type::LValueReference:
+      case Type::RValueReference:
+      case Type::Pointer:
+      case Type::MemberPointer:
+      case Type::ObjCInterface:
+      case Type::ExtInt:
+      case Type::Record:
+      case Type::Pipe:
+        break;
+      }
+      return isTypedef;
+    };
+
     auto performBuiltinTypeAlignmentUpgrade = [&](const BuiltinType *BTy) {
       if (BTy->getKind() == BuiltinType::Double ||
           BTy->getKind() == BuiltinType::LongDouble) {
@@ -1989,16 +2125,25 @@
       }
     };
 
-    const Type *Ty = D->getType()->getBaseElementTypeUnsafe();
-    if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
-      performBuiltinTypeAlignmentUpgrade(CTy->getElementType()->castAs<BuiltinType>());
-    } else if (const BuiltinType *BTy = Ty->getAs<BuiltinType>()) {
-      performBuiltinTypeAlignmentUpgrade(BTy);
-    } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
-      const RecordDecl *RD = RT->getDecl();
-      assert(RD && "Expected non-null RecordDecl.");
-      const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD);
-      PreferredAlign = FieldRecord.getPreferredAlignment();
+    const Type *Ty = D->getType().getTypePtr();
+    const Type *BaseTy = Ty->getBaseElementTypeUnsafe();
+    // When used as part of a typedef, or together with a 'packed' attribute,
+    // the 'aligned' attribute can be used to decrease alignment. In that case,
+    // it overrides any computed alignment we have, and there is no need to
+    // upgrade the alignment.
+    if (!(AlignIsRequired &&
+          (checkTypeIsTypedef(checkTypeIsTypedef, Ty) || FieldPacked))) {
+      if (const ComplexType *CTy = BaseTy->getAs<ComplexType>()) {
+        performBuiltinTypeAlignmentUpgrade(
+            CTy->getElementType()->castAs<BuiltinType>());
+      } else if (const BuiltinType *BTy = BaseTy->getAs<BuiltinType>()) {
+        performBuiltinTypeAlignmentUpgrade(BTy);
+      } else if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
+        const RecordDecl *RD = RT->getDecl();
+        assert(RD && "Expected non-null RecordDecl.");
+        const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD);
+        PreferredAlign = FieldRecord.getPreferredAlignment();
+      }
     }
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to