Skip to content

[clang][dataflow] Transfer more cast expressions. #153066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/include/clang/Analysis/FlowSensitive/StorageLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h"
#include <cassert>

Expand Down Expand Up @@ -152,6 +153,11 @@ class RecordStorageLocation final : public StorageLocation {
return {SyntheticFields.begin(), SyntheticFields.end()};
}

/// Add a synthetic field, if none by that name is already present.
void addSyntheticField(llvm::StringRef Name, StorageLocation &Loc) {
SyntheticFields.insert({Name, &Loc});
}

/// Changes the child storage location for a field `D` of reference type.
/// All other fields cannot change their storage location and always retain
/// the storage location passed to the `RecordStorageLocation` constructor.
Expand All @@ -164,6 +170,11 @@ class RecordStorageLocation final : public StorageLocation {
Children[&D] = Loc;
}

/// Add a child storage location for a field `D`, if not already present.
void addChild(const ValueDecl &D, StorageLocation *Loc) {
Children.insert({&D, Loc});
}

llvm::iterator_range<FieldToLoc::const_iterator> children() const {
return {Children.begin(), Children.end()};
}
Expand Down
71 changes: 59 additions & 12 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
#include "clang/AST/OperationKinds.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/Type.h"
#include "clang/Analysis/FlowSensitive/ASTOps.h"
#include "clang/Analysis/FlowSensitive/AdornedCFG.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/NoopAnalysis.h"
#include "clang/Analysis/FlowSensitive/RecordOps.h"
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
#include "clang/Analysis/FlowSensitive/Value.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/OperatorKinds.h"
#include "llvm/Support/Casting.h"
#include <assert.h>
Expand Down Expand Up @@ -287,7 +290,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
}
}

void VisitImplicitCastExpr(const ImplicitCastExpr *S) {
void VisitCastExpr(const CastExpr *S) {
const Expr *SubExpr = S->getSubExpr();
assert(SubExpr != nullptr);

Expand Down Expand Up @@ -317,17 +320,70 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
break;
}

case CK_BaseToDerived: {
// This is a cast of (single-layer) pointer or reference to a record type.
// We should now model the fields for the derived type.

// Get the RecordStorageLocation for the record object underneath.
RecordStorageLocation *Loc = nullptr;
if (S->getType()->isPointerType()) {
auto *PV = Env.get<PointerValue>(*SubExpr);
assert(PV != nullptr);
if (PV == nullptr)
break;
Loc = cast<RecordStorageLocation>(&PV->getPointeeLoc());
} else {
assert(S->getType()->isRecordType());
if (SubExpr->isGLValue()) {
Loc = Env.get<RecordStorageLocation>(*SubExpr);
} else {
Loc = &Env.getResultObjectLocation(*SubExpr);
}
}
if (!Loc) {
// Nowhere to add children or propagate from, so we're done.
break;
}

// Get the derived record type underneath the reference or pointer.
QualType Derived = S->getType().getNonReferenceType();
if (Derived->isPointerType()) {
Derived = Derived->getPointeeType();
}

// Add children to the storage location for fields (including synthetic
// fields) of the derived type and initialize their values.
for (const FieldDecl *Field :
Env.getDataflowAnalysisContext().getModeledFields(Derived)) {
assert(Field != nullptr);
QualType FieldType = Field->getType();
if (FieldType->isReferenceType()) {
Loc->addChild(*Field, nullptr);
} else {
Loc->addChild(*Field, &Env.createStorageLocation(FieldType));
}

for (const auto &Entry :
Env.getDataflowAnalysisContext().getSyntheticFields(Derived)) {
Loc->addSyntheticField(Entry.getKey(),
Env.createStorageLocation(Entry.getValue()));
}
}
Env.initializeFieldsWithValues(*Loc, Derived);

// Fall through to propagate SubExpr's StorageLocation to the CastExpr.
[[fallthrough]];
}
case CK_IntegralCast:
// FIXME: This cast creates a new integral value from the
// subexpression. But, because we don't model integers, we don't
// distinguish between this new value and the underlying one. If integer
// modeling is added, then update this code to create a fresh location and
// value.
case CK_UncheckedDerivedToBase:
case CK_DerivedToBase:
case CK_ConstructorConversion:
case CK_UserDefinedConversion:
// FIXME: Add tests that excercise CK_UncheckedDerivedToBase,
// CK_ConstructorConversion, and CK_UserDefinedConversion.
case CK_NoOp: {
// FIXME: Consider making `Environment::getStorageLocation` skip noop
// expressions (this and other similar expressions in the file) instead
Expand Down Expand Up @@ -684,15 +740,6 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
propagateValue(*SubExpr, *S, Env);
}

void VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
if (S->getCastKind() == CK_NoOp) {
const Expr *SubExpr = S->getSubExpr();
assert(SubExpr != nullptr);

propagateValueOrStorageLocation(*SubExpr, *S, Env);
}
}

void VisitConditionalOperator(const ConditionalOperator *S) {
const Environment *TrueEnv = StmtToEnv.getEnvironment(*S->getTrueExpr());
const Environment *FalseEnv = StmtToEnv.getEnvironment(*S->getFalseExpr());
Expand Down
Loading