25 const MatchFinder::MatchResult &Result) {
27 const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>(
"new");
29 const QualType T = NewExpr->getAllocatedType();
31 if (T->isDependentType())
33 const TagDecl *D = T->getAsTagDecl();
35 if (!D || !D->isCompleteDefinition())
38 const ASTContext &Context = D->getASTContext();
41 if (!Context.isAlignmentRequired(T))
45 const unsigned SpecifiedAlignment = D->getMaxAlignment();
47 if (!SpecifiedAlignment)
50 const unsigned DefaultNewAlignment = Context.getTargetInfo().getNewAlign();
52 const bool OverAligned = SpecifiedAlignment > DefaultNewAlignment;
53 const bool HasDefaultOperatorNew =
54 !NewExpr->getOperatorNew() || NewExpr->getOperatorNew()->isImplicit();
56 const unsigned CharWidth = Context.getTargetInfo().getCharWidth();
57 if (HasDefaultOperatorNew && OverAligned)
58 diag(NewExpr->getBeginLoc(),
59 "allocation function returns a pointer with alignment %0 but the "
60 "over-aligned type being allocated requires alignment %1")
61 << (DefaultNewAlignment / CharWidth)
62 << (SpecifiedAlignment / CharWidth);