[PATCH] D83317: [Sema] Teach -Wcast-align to compute alignment of CXXThisExpr

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 7 08:53:00 PDT 2020


ahatanak created this revision.
ahatanak added reviewers: rjmccall, jyknight.
ahatanak added a project: clang.
Herald added subscribers: ributzka, dexonsmith, jkorous.

This fixes https://bugs.llvm.org/show_bug.cgi?id=46605.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83317

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/warn-cast-align.cpp


Index: clang/test/SemaCXX/warn-cast-align.cpp
===================================================================
--- clang/test/SemaCXX/warn-cast-align.cpp
+++ clang/test/SemaCXX/warn-cast-align.cpp
@@ -44,9 +44,16 @@
   c = IntPtr(P);
 }
 
+struct __attribute__((aligned(16))) AlignedS {
+  char m[16];
+};
+
 struct __attribute__((aligned(16))) A {
   char m0[16];
   char m1[16];
+  AlignedS *getAlignedS() {
+    return (AlignedS *)m1;
+  }
 };
 
 struct B0 {
@@ -92,6 +99,9 @@
 
 struct D5 : virtual D0 {
   char m0[16];
+  AlignedS *get() {
+    return (AlignedS *)m0; // expected-warning {{cast from 'char *' to 'AlignedS *'}}
+  }
 };
 
 struct D6 : virtual D5 {
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -13487,12 +13487,14 @@
   }
   case Stmt::MemberExprClass: {
     auto *ME = cast<MemberExpr>(E);
-    if (ME->isArrow())
-      break;
     auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
     if (!FD || FD->getType()->isReferenceType())
       break;
-    auto P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx);
+    Optional<std::pair<CharUnits, CharUnits>> P;
+    if (ME->isArrow())
+      P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx);
+    else
+      P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx);
     if (!P)
       break;
     const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent());
@@ -13556,6 +13558,11 @@
     }
     break;
   }
+  case Stmt::CXXThisExprClass: {
+    auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl();
+    CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment();
+    return std::make_pair(Alignment, CharUnits::Zero());
+  }
   case Stmt::UnaryOperatorClass: {
     auto *UO = cast<UnaryOperator>(E);
     if (UO->getOpcode() == UO_AddrOf)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83317.276086.patch
Type: text/x-patch
Size: 1916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200707/8b230ba4/attachment.bin>


More information about the cfe-commits mailing list