[clang] 2946b25 - [Clang] Fix crash when checking misaligned member with dependent type

Jun Zhang via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 18 06:06:56 PDT 2022


Author: Jun Zhang
Date: 2022-10-18T21:05:49+08:00
New Revision: 2946b252993d9b4c12479bf318b648bed2c0427a

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

LOG: [Clang] Fix crash when checking misaligned member with dependent type

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

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

Added: 
    clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp

Modified: 
    clang/lib/Sema/SemaChecking.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index c0ecd7b091000..9d8640a6a057a 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -17387,15 +17387,15 @@ void Sema::DiagnoseMisalignedMembers() {
 
 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) {
   E = E->IgnoreParens();
-  if (!T->isPointerType() && !T->isIntegerType())
+  if (!T->isPointerType() && !T->isIntegerType() && !T->isDependentType())
     return;
   if (isa<UnaryOperator>(E) &&
       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));
       if (MA != MisalignedMembers.end() &&
-          (T->isIntegerType() ||
+          (T->isDependentType() || T->isIntegerType() ||
            (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
                                    Context.getTypeAlignInChars(
                                        T->getPointeeType()) <= MA->Alignment))))

diff  --git a/clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp b/clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
new file mode 100644
index 0000000000000..cd4768350bab4
--- /dev/null
+++ b/clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+struct __attribute__((packed)) {
+  unsigned options;
+  template <typename T>
+  void getOptions() {
+      (T *)&options;
+  }
+  template <typename U>
+	void getOptions2() {
+      (U)&options;
+	}
+} s;
+
+struct __attribute__((packed)) { // expected-error {{anonymous structs and classes must be class members}}
+           unsigned options ;
+  template <typename T> getOptions() // expected-error {{a type specifier is required for all declarations}}
+    {
+      (T *) & options;
+    }
+};


        


More information about the cfe-commits mailing list