[clang] [llvm] [NFC][analyzer] Extract bounds checking library (PR #202372)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 29 09:56:13 PDT 2026


================
@@ -0,0 +1,211 @@
+//===- BoundsChecking.h - Bounds checking related APIs ----------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines APIs for performing a bounds check (i.e. comparing a
+//  symbolic Offset value to zero and a symbolic Extent value) and composing
+//  descriptions that explain its results.
+//
+//  This is intended as a replacement for `ProgramState::assumeInBound` to
+//  avoid its incorrect logic and compensate for deficiencies of other parts of
+//  the analyzer.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BOUNDSCHECKING_H
+#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BOUNDSCHECKING_H
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+#include "llvm/Support/FormatVariadic.h"
+#include <optional>
+
+namespace clang {
+namespace ento {
+
+/// If `E` is an array subscript expression with a base that is "clean" (= not
+/// modified by pointer arithmetic = the beginning of a memory region), return
+/// it as a pointer to ArraySubscriptExpr; otherwise return nullptr.
+/// This helper function is used by two separate heuristics that are only valid
+/// in these "clean" cases.
+const ArraySubscriptExpr *getAsCleanArraySubscriptExpr(const Expr *E,
----------------
NagyDonat wrote:

> Is this API ever useful for the clients? If it is an implementation detail it should not be in a public header.

You're right that this is an oddly specific helper function, but it is also used by `ArrayBoundChecker.cpp` (to implement `isObviouslyNonnegative`) in addition to the use in `BoundsChecking.h` (the inline definition of `SizeUnit::forExpr`).

As I don't want to duplicate its logic in these two TUs, I think the least bad approach is declaring it here in this header. (If it was a bit shorter, I would duplicate it.)

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


More information about the cfe-commits mailing list