junaire created this revision.
junaire added reviewers: aaron.ballman, shafik.
Herald added a project: All.
junaire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If the type is dependent, we should just discard it and not checking its
alignment as it doesn't exisit yet.
Fixes https://github.com/llvm/llvm-project/issues/58370


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136018

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp


Index: clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+struct __attribute__((packed)) {
+  unsigned options;
+  template <typename T>
+  void getOptions() {
+      (T *)&options;
+  }
+} s;
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -17393,12 +17393,13 @@
       cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) {
     auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
     if (isa<MemberExpr>(Op)) {
-      auto MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
+      auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
+      const bool IsDiscardMisalignedPointer =
+          T->isPointerType() &&
+          (T->getPointeeType()->isIncompleteType() || T->isDependentType() ||
+           Context.getTypeAlignInChars(T->getPointeeType()) <= MA->Alignment);
       if (MA != MisalignedMembers.end() &&
-          (T->isIntegerType() ||
-           (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
-                                   Context.getTypeAlignInChars(
-                                       T->getPointeeType()) <= 
MA->Alignment))))
+          (T->isIntegerType() || IsDiscardMisalignedPointer))
         MisalignedMembers.erase(MA);
     }
   }


Index: clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+struct __attribute__((packed)) {
+  unsigned options;
+  template <typename T>
+  void getOptions() {
+      (T *)&options;
+  }
+} s;
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -17393,12 +17393,13 @@
       cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) {
     auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
     if (isa<MemberExpr>(Op)) {
-      auto MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
+      auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
+      const bool IsDiscardMisalignedPointer =
+          T->isPointerType() &&
+          (T->getPointeeType()->isIncompleteType() || T->isDependentType() ||
+           Context.getTypeAlignInChars(T->getPointeeType()) <= MA->Alignment);
       if (MA != MisalignedMembers.end() &&
-          (T->isIntegerType() ||
-           (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
-                                   Context.getTypeAlignInChars(
-                                       T->getPointeeType()) <= MA->Alignment))))
+          (T->isIntegerType() || IsDiscardMisalignedPointer))
         MisalignedMembers.erase(MA);
     }
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to