[clang] [clang] Strict aliasing warning ala GCC [PR50066] (PR #74155)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 6 05:01:45 PST 2023


================
@@ -498,3 +498,137 @@ CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
   // access type regardless of their base types.
   return TBAAAccessInfo::getMayAliasInfo();
 }
+
+// Determine the aliasing kind bit-converting from type Src to type Dst.
+CodeGenTBAA::AliasingKind CodeGenTBAA::getAliasingKind(QualType &Dst,
+                                                       QualType &Src) {
+  assert(!Src->isVoidType() && !Dst->isVoidType());
+  if (TypeHasMayAlias(Src) || TypeHasMayAlias(Dst))
+    return AK_Ok;
+
+  Src = QualType{Src->getBaseElementTypeUnsafe(), 0};
+  Dst = QualType{Dst->getBaseElementTypeUnsafe(), 0};
+
+  auto *SrcDecl = Src->getAsRecordDecl();
+  auto *DstDecl = Dst->getAsRecordDecl();
+
+  const llvm::MDNode *AnyTBAA = getChar();
+  const llvm::MDNode *SrcTBAA = nullptr;
+  const llvm::MDNode *DstTBAA = nullptr;
+
+  if (!SrcDecl) {
+    SrcTBAA = getTypeInfo(Src);
+    if (!SrcTBAA || SrcTBAA == AnyTBAA)
+      return AK_Ok;
+  }
+  if (!DstDecl) {
+    DstTBAA = getTypeInfo(Dst);
+    if (!DstTBAA || DstTBAA == AnyTBAA)
+      return AK_Ok;
+  }
+
+  auto IsAncestor = [](const llvm::MDNode *Ancestor,
+                       const llvm::MDNode *Descendant) {
+    assert(Ancestor != Descendant && "Identical TBAA");
+    while (Descendant->getNumOperands() != 1) {
+      Descendant = cast<llvm::MDNode>(Descendant->getOperand(1));
+      if (Descendant == Ancestor)
+        return true;
+    }
+    return false;
+  };
+
+  assert(SrcTBAA != AnyTBAA && DstTBAA != AnyTBAA &&
----------------
Endilll wrote:

Sure, I'm not insisting.

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


More information about the cfe-commits mailing list