[clang] [Clang] Optimize -Wunsafe-buffer-usage. (PR #125492)
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 11 22:04:56 PDT 2025
================
@@ -1139,26 +1276,30 @@ class ArraySubscriptGadget : public WarningGadget {
const ArraySubscriptExpr *ASE;
public:
- ArraySubscriptGadget(const MatchFinder::MatchResult &Result)
+ ArraySubscriptGadget(const MatchResult &Result)
: WarningGadget(Kind::ArraySubscript),
- ASE(Result.Nodes.getNodeAs<ArraySubscriptExpr>(ArraySubscrTag)) {}
+ ASE(Result.getNodeAs<ArraySubscriptExpr>(ArraySubscrTag)) {}
static bool classof(const Gadget *G) {
return G->getKind() == Kind::ArraySubscript;
}
- static Matcher matcher() {
- // clang-format off
- return stmt(arraySubscriptExpr(
- hasBase(ignoringParenImpCasts(
- anyOf(hasPointerType(), hasArrayType()))),
- unless(anyOf(
- isSafeArraySubscript(),
- hasIndex(
- anyOf(integerLiteral(equals(0)), arrayInitIndexExpr())
- )
- ))).bind(ArraySubscrTag));
- // clang-format on
+ static bool matches(const Stmt *S, const ASTContext &Ctx,
+ MatchResult &Result) {
+ const auto *ASE = dyn_cast<ArraySubscriptExpr>(S);
+ if (!ASE)
+ return false;
+ const auto *const Base = ASE->getBase()->IgnoreParenImpCasts();
+ if (!hasPointerType(*Base) && !hasArrayType(*Base))
+ return false;
+ bool IsSafeIndex =
+ (isa<IntegerLiteral>(ASE->getIdx()) &&
----------------
ziqingluo-90 wrote:
nit: maybe combine `isa<IntegerLiteral>` and `cast<IntegerLiteral>` to a `dyn_cast`?
https://github.com/llvm/llvm-project/pull/125492
More information about the cfe-commits
mailing list