clang 23.0.0git
clang::lifetimes::internal::OriginManager Class Reference

Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions. More...

#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"

Public Member Functions

 OriginManager (const AnalysisDeclContext &AC)
OriginListgetOrCreateList (const ValueDecl *D)
 Gets or creates the OriginList for a given ValueDecl.
OriginListgetOrCreateList (const Expr *E)
 Gets or creates the OriginList for a given Expr.
OriginListcreateSingleOriginList (OriginID OID)
 Wraps an existing OriginID in a new single-element OriginList, so a fact can refer to a single level of an existing OriginList.
std::optional< OriginList * > getThisOrigins () const
 Returns the OriginList for the implicit 'this' parameter if the current declaration is an instance method.
const OrigingetOrigin (OriginID ID) const
llvm::ArrayRef< OrigingetOrigins () const
unsigned getNumOrigins () const
bool hasOrigins (QualType QT, bool IntrinsicOnly=false) const
 Determines whether a type can carry lifetime origins.
bool hasOrigins (const Expr *E) const
 Determines if an expression has origins that need to be tracked.
void dump (OriginID OID, llvm::raw_ostream &OS) const
void collectMissingOrigins (Stmt &FunctionBody, LifetimeSafetyStats &LSStats)
 Collects statistics about expressions that lack associated origins.

Detailed Description

Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions.

Definition at line 125 of file Origins.h.

Constructor & Destructor Documentation

◆ OriginManager()

clang::lifetimes::internal::OriginManager::OriginManager ( const AnalysisDeclContext & AC)
explicit

Definition at line 165 of file Origins.cpp.

References clang::AnalysisDeclContext::getDecl().

Member Function Documentation

◆ collectMissingOrigins()

void clang::lifetimes::internal::OriginManager::collectMissingOrigins ( Stmt & FunctionBody,
LifetimeSafetyStats & LSStats )

Collects statistics about expressions that lack associated origins.

Definition at line 315 of file Origins.cpp.

Referenced by clang::lifetimes::internal::collectLifetimeStats().

◆ createSingleOriginList()

OriginList * clang::lifetimes::internal::OriginManager::createSingleOriginList ( OriginID OID)

Wraps an existing OriginID in a new single-element OriginList, so a fact can refer to a single level of an existing OriginList.

Definition at line 194 of file Origins.cpp.

◆ dump()

◆ getNumOrigins()

unsigned clang::lifetimes::internal::OriginManager::getNumOrigins ( ) const
inline

Definition at line 159 of file Origins.h.

Referenced by clang::lifetimes::internal::computePersistentOrigins().

◆ getOrCreateList() [1/2]

OriginList * clang::lifetimes::internal::OriginManager::getOrCreateList ( const Expr * E)

Gets or creates the OriginList for a given Expr.

Creates a list based on the expression's type and value category:

  • Lvalues get an implicit reference level (modeling addressability)
  • Rvalues of non-pointer type return nullptr (no trackable origin)
  • DeclRefExpr may reuse the underlying declaration's list
Returns
The OriginList, or nullptr for non-pointer rvalues.

Definition at line 225 of file Origins.cpp.

References clang::lifetimes::internal::doesDeclHaveStorage(), getOrCreateList(), clang::Expr::getType(), hasOrigins(), clang::Expr::IgnoreParens(), clang::isa(), clang::Expr::isGLValue(), clang::Type::isReferenceType(), and clang::lifetimes::internal::OriginList::setInnerOriginList().

◆ getOrCreateList() [2/2]

OriginList * clang::lifetimes::internal::OriginManager::getOrCreateList ( const ValueDecl * D)

Gets or creates the OriginList for a given ValueDecl.

Creates a list structure mirroring the levels of indirection in the declaration's type (e.g., int** p creates list of size 2).

Returns
The OriginList, or nullptr if the type is not pointer-like.

Definition at line 216 of file Origins.cpp.

References clang::ValueDecl::getType(), and hasOrigins().

Referenced by getOrCreateList().

◆ getOrigin()

const Origin & clang::lifetimes::internal::OriginManager::getOrigin ( OriginID ID) const

Definition at line 310 of file Origins.cpp.

Referenced by dump().

◆ getOrigins()

llvm::ArrayRef< Origin > clang::lifetimes::internal::OriginManager::getOrigins ( ) const
inline

Definition at line 157 of file Origins.h.

◆ getThisOrigins()

std::optional< OriginList * > clang::lifetimes::internal::OriginManager::getThisOrigins ( ) const
inline

Returns the OriginList for the implicit 'this' parameter if the current declaration is an instance method.

Definition at line 153 of file Origins.h.

◆ hasOrigins() [1/2]

bool clang::lifetimes::internal::OriginManager::hasOrigins ( const Expr * E) const

Determines if an expression has origins that need to be tracked.

An expression has origins if:

  • It's a glvalue (has addressable storage), OR
  • Its type is pointer-like (pointer, reference, or gsl::Pointer), OR
  • Its type is registered for origin tracking (e.g., return type of a [[clang::lifetimebound]] function)

Examples:

  • int x; x : has origin (glvalue)
  • int* p; p : has 2 origins (1 for glvalue and 1 for pointer type)
  • std::string_view{} : has 1 origin (prvalue of pointer type)
  • 42 : no origin (prvalue of non-pointer type)
  • x + y : (where x, y are int) → no origin (prvalue of non-pointer type)

Definition at line 144 of file Origins.cpp.

References clang::Expr::getType(), hasOrigins(), and clang::Expr::isGLValue().

◆ hasOrigins() [2/2]

bool clang::lifetimes::internal::OriginManager::hasOrigins ( QualType QT,
bool IntrinsicOnly = false ) const

Determines whether a type can carry lifetime origins.

Parameters
QTThe type to check.
IntrinsicOnlyIf true, only consider types that can intrinsically carry origins. If false, also include types that are tracked due to context-sensitive annotations (e.g., return types of [[clang::lifetimebound]] functions).

Intrinsic origin types:

  • Pointer types (int*, void*)
  • Reference types (int&, const T&)
  • gsl::Pointer annotated types (std::string_view)
  • Lambdas capturing pointer-like objects
  • Standard callable wrappers (std::function)

TODO: Expand this list with other origin types such as: user-defined structs with pointer-like fields.

Contextual origin types (excluded when IntrinsicOnly=true):

  • Types appearing as return values of functions with [[clang::lifetimebound]] parameters, stored in LifetimeAnnotatedOriginTypes during function body analysis.

Definition at line 103 of file Origins.cpp.

References clang::Type::getAs(), clang::Type::getAsCXXRecordDecl(), clang::QualType::getCanonicalType(), clang::QualType::getTypePtr(), hasOrigins(), clang::lifetimes::isGslPointerType(), clang::Type::isPointerOrReferenceType(), and clang::lifetimes::isStdCallableWrapperType().

Referenced by getOrCreateList(), getOrCreateList(), hasOrigins(), and hasOrigins().


The documentation for this class was generated from the following files: