clang 22.0.0git
InferAlloc.cpp File Reference
#include "clang/AST/InferAlloc.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Type.h"
#include "clang/Basic/IdentifierTable.h"
#include "llvm/ADT/SmallPtrSet.h"

Go to the source code of this file.

Functions

static bool typeContainsPointer (QualType T, llvm::SmallPtrSet< const RecordDecl *, 4 > &VisitedRD, bool &IncompleteType)
static QualType inferTypeFromSizeofExpr (const Expr *E)
 Infer type from a simple sizeof expression.
static QualType inferPossibleTypeFromArithSizeofExpr (const Expr *E)
 Infer type from an arithmetic expression involving a sizeof.
static QualType inferPossibleTypeFromVarInitSizeofExpr (const Expr *E)
 If the expression E is a reference to a variable, infer the type from a variable's initializer if it contains a sizeof.
static QualType inferPossibleTypeFromCastExpr (const CallExpr *CallE, const CastExpr *CastE)
 Deduces the allocated type by checking if the allocation call's result is immediately used in a cast expression.

Function Documentation

◆ inferPossibleTypeFromArithSizeofExpr()

QualType inferPossibleTypeFromArithSizeofExpr ( const Expr * E)
static

Infer type from an arithmetic expression involving a sizeof.

For example:

malloc(sizeof(MyType) + padding); // infers 'MyType' malloc(sizeof(MyType) * 32); // infers 'MyType' malloc(32 * sizeof(MyType)); // infers 'MyType' malloc(sizeof(MyType) << 1); // infers 'MyType' ...

More complex arithmetic expressions are supported, but are a heuristic, e.g. when considering allocations for structs with flexible array members:

malloc(sizeof(HasFlexArray) + sizeof(int) * 32); // infers 'HasFlexArray'

Definition at line 101 of file InferAlloc.cpp.

References clang::Expr::IgnoreParenImpCasts(), inferPossibleTypeFromArithSizeofExpr(), inferTypeFromSizeofExpr(), and clang::T.

Referenced by clang::infer_alloc::inferPossibleType(), inferPossibleTypeFromArithSizeofExpr(), and inferPossibleTypeFromVarInitSizeofExpr().

◆ inferPossibleTypeFromCastExpr()

QualType inferPossibleTypeFromCastExpr ( const CallExpr * CallE,
const CastExpr * CastE )
static

Deduces the allocated type by checking if the allocation call's result is immediately used in a cast expression.

For example:

MyType *x = (MyType *)malloc(4096); // infers 'MyType'

Definition at line 153 of file InferAlloc.cpp.

References clang::Type::getPointeeType(), clang::Expr::getType(), and clang::Type::isPointerType().

Referenced by clang::infer_alloc::inferPossibleType().

◆ inferPossibleTypeFromVarInitSizeofExpr()

QualType inferPossibleTypeFromVarInitSizeofExpr ( const Expr * E)
static

If the expression E is a reference to a variable, infer the type from a variable's initializer if it contains a sizeof.

Beware, this is a heuristic and ignores if a variable is later reassigned. For example:

size_t my_size = sizeof(MyType); void *x = malloc(my_size); // infers 'MyType'

Definition at line 137 of file InferAlloc.cpp.

References clang::Expr::IgnoreParenImpCasts(), inferPossibleTypeFromArithSizeofExpr(), and clang::Init.

Referenced by clang::infer_alloc::inferPossibleType().

◆ inferTypeFromSizeofExpr()

QualType inferTypeFromSizeofExpr ( const Expr * E)
static

Infer type from a simple sizeof expression.

Definition at line 75 of file InferAlloc.cpp.

References clang::Expr::IgnoreParenImpCasts().

Referenced by inferPossibleTypeFromArithSizeofExpr().

◆ typeContainsPointer()