[clang] [clang][ssaf] Add cpp-bounded-buffers source transformation (PR #210457)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 12 06:46:26 PDT 2026


================
@@ -0,0 +1,77 @@
+//===- CppBoundedBuffers.h --------------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// The cpp-bounded-buffers transformation rewrites buffers -- raw pointers and
+// arrays -- reachable from unsafe buffer usage into bounded types
+// (bounded_ptr<T>, bounded_array<T, N>). Reachable declarators that are not
+// rewritten are reported instead, so no reachable buffer is silently left raw.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_SCALABLESTATICANALYSIS_SOURCETRANSFORMATION_TRANSFORMATIONS_CPPBOUNDEDBUFFERS_H
+#define LLVM_CLANG_SCALABLESTATICANALYSIS_SOURCETRANSFORMATION_TRANSFORMATIONS_CPPBOUNDEDBUFFERS_H
+
+#include "clang/AST/Type.h"
+#include "clang/ScalableStaticAnalysis/SourceTransformation/Transformation.h"
+#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringRef.h"
+#include <optional>
+#include <string>
+
+namespace clang {
+class ASTContext;
+} // namespace clang
+
+namespace clang::ssaf {
+
+/// The bounded type a raw declarator is rewritten to.
+enum class BoundedType { Ptr, Array };
+
+/// Why a reachable declarator was reported instead of rewritten.
+enum class ReportReason {
+  MultiLevelPointer,
+  PointerToArray,
+  ReferenceToPointer,
+  MultiDimensionalArray,
+  IncompleteArray,
+  UnreproducibleType,
+  DeclarationGroup,
+  MacroExpansion,
+  TrailingReturnType,
+  EmissionFailed,
+  NotTransformed,
+};
+
+/// Returns the report message for \p Reason.
+llvm::StringRef messageFor(ReportReason Reason);
+
+/// The outcome of classifying a declared type against the reachable pointer
+/// levels of its entity: a bounded-type rewrite, a report reason, or neither.
+struct ClassifyResult {
+  std::optional<BoundedType> NewType;
+  // Pointee/element spelling; meaningful only when NewType is set.
+  std::string InnerSpelling;
+  std::optional<ReportReason> Skip;
+};
+
+/// Classifies the declared type \p T of a reachable entity. \p ReachableLevels
+/// holds the entity's reachable pointer levels (1-based, outermost is level 1).
+ClassifyResult
+classifyDeclType(QualType T, const llvm::SmallSet<unsigned, 4> &ReachableLevels,
+                 const ASTContext &Ctx);
+
+class CppBoundedBuffers : public Transformation {
----------------
steakhal wrote:

Is this expected to be `final`?

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


More information about the cfe-commits mailing list