[clang] [clang] Strict aliasing warning ala GCC [PR50066] (PR #74155)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 3 03:03:52 PST 2023
================
@@ -2026,6 +2027,137 @@ static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr,
return TC_Success;
}
+// We're dereferencing E, either by turning into an RValue, or by dereferencing
+// it. Check whether it's a deref of a reinterpret cast that has aliasing
+// issues.
+void Sema::CheckStrictAliasingDeref(Expr const *E, bool IsLValue) {
+ if (Diags.getDiagnosticOptions().StrictAliasing < 3)
+ return;
+
+ assert(IsLValue || E->getType()->isAnyPointerType());
+ CastExpr const *CE = nullptr;
+ for (;;) {
+ CE = dyn_cast<CastExpr>(E->IgnoreParens());
+ if (!CE)
+ return;
+
+ if (IsLValue || CE->getCastKind() != CK_ArrayToPointerDecay)
+ break;
+
+ E = CE->getSubExpr();
+ IsLValue = true;
+ }
+
+ if (CE->getCastKind() != (IsLValue ? CK_LValueBitCast : CK_BitCast))
+ return;
+
+ if (CE->getSubExpr()->getType()->isVoidPointerType())
+ return;
+
+ QualType DestTy = CE->getType();
+ if (!IsLValue)
+ DestTy = DestTy->getPointeeType();
+
+ CheckStrictAliasing(CE->getSubExpr(), DestTy, IsLValue, CE->getSourceRange());
+}
+
+/// We're building a cast from E to pointer type DestType. If ISLValueCast is
+/// true, DestType is the pointer equivalent of the reference type we're casting
+/// to.
+void Sema::CheckStrictAliasingCast(Expr const *E, QualType DestType,
+ bool IsLValueCast, SourceRange Range) {
+ if (Diags.getDiagnosticOptions().StrictAliasing < 1 ||
----------------
Endilll wrote:
Switch might be a better fit here.
https://github.com/llvm/llvm-project/pull/74155
More information about the cfe-commits
mailing list