================ @@ -1349,6 +1350,98 @@ void CodeGenFunction::EmitAllocTokenHint(llvm::CallBase *CB, CB->setMetadata(llvm::LLVMContext::MD_alloc_token_hint, MDN); } +/// Infer type from a simple sizeof expression. +static QualType inferTypeFromSizeofExpr(const Expr *E) { + const Expr *Arg = E->IgnoreParenImpCasts(); + if (const auto *UET = dyn_cast<UnaryExprOrTypeTraitExpr>(Arg)) { + if (UET->getKind() == UETT_SizeOf) { + if (UET->isArgumentType()) { + return UET->getArgumentTypeInfo()->getType(); + } else { + return UET->getArgumentExpr()->getType(); + } + } + } + return QualType(); +} + +/// Infer type from an arithmetic expression involving a sizeof. +static QualType inferTypeFromArithSizeofExpr(const Expr *E) { + const Expr *Arg = E->IgnoreParenImpCasts(); + // The argument is a lone sizeof expression. + QualType QT = inferTypeFromSizeofExpr(Arg); + if (!QT.isNull()) + return QT; + if (const auto *BO = dyn_cast<BinaryOperator>(Arg)) { + // Argument is an arithmetic expression. Cover common arithmetic patterns + // involving sizeof. + switch (BO->getOpcode()) { + case BO_Add: + case BO_Div: + case BO_Mul: + case BO_Shl: + case BO_Shr: + case BO_Sub: + QT = inferTypeFromArithSizeofExpr(BO->getLHS()); + if (!QT.isNull()) + return QT; + QT = inferTypeFromArithSizeofExpr(BO->getRHS()); + if (!QT.isNull()) + return QT; + break; + default: + break; + } + } + return QualType(); +} + +/// If the expression E is a reference to a variable, infer the type from a +/// variable's initializer if it contains a sizeof. Beware, this is a heuristic +/// and ignores if a variable is later reassigned. +static QualType inferTypeFromVarInitSizeofExpr(const Expr *E) { + const Expr *Arg = E->IgnoreParenImpCasts(); + if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg)) { + if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { + if (const Expr *Init = VD->getInit()) { ---------------- fmayer wrote:
remove braces https://github.com/llvm/llvm-project/pull/156841 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits