2021-12-29 11:31:02 +00:00
|
|
|
//===-- Transfer.cpp --------------------------------------------*- 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 transfer functions that evaluate program statements and
|
|
|
|
// update an environment accordingly.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/FlowSensitive/Transfer.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
2022-01-13 13:53:52 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
2022-01-11 12:15:53 +00:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2022-01-04 13:47:14 +00:00
|
|
|
#include "clang/AST/OperationKinds.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2022-07-26 17:54:13 +00:00
|
|
|
#include "clang/Analysis/FlowSensitive/ControlFlowContext.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
|
2022-07-26 17:54:13 +00:00
|
|
|
#include "clang/Analysis/FlowSensitive/NoopAnalysis.h"
|
2023-06-20 08:00:01 +00:00
|
|
|
#include "clang/Analysis/FlowSensitive/RecordOps.h"
|
2022-03-25 20:01:18 +00:00
|
|
|
#include "clang/Analysis/FlowSensitive/Value.h"
|
2022-04-01 12:51:23 +00:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2022-01-04 13:47:14 +00:00
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
2022-01-24 16:17:22 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
2022-10-06 17:56:41 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include <cassert>
|
2022-01-04 13:47:14 +00:00
|
|
|
#include <memory>
|
2022-01-24 16:17:22 +00:00
|
|
|
#include <tuple>
|
2021-12-29 11:31:02 +00:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace dataflow {
|
|
|
|
|
2023-03-28 07:56:24 +00:00
|
|
|
const Environment *StmtToEnvMap::getEnvironment(const Stmt &S) const {
|
|
|
|
auto BlockIt = CFCtx.getStmtToBlock().find(&ignoreCFGOmittedNodes(S));
|
|
|
|
assert(BlockIt != CFCtx.getStmtToBlock().end());
|
|
|
|
if (!CFCtx.isBlockReachable(*BlockIt->getSecond()))
|
|
|
|
return nullptr;
|
|
|
|
const auto &State = BlockToState[BlockIt->getSecond()->getBlockID()];
|
|
|
|
assert(State);
|
|
|
|
return &State->Env;
|
|
|
|
}
|
|
|
|
|
2022-03-25 20:01:18 +00:00
|
|
|
static BoolValue &evaluateBooleanEquality(const Expr &LHS, const Expr &RHS,
|
|
|
|
Environment &Env) {
|
2023-06-15 19:07:05 +00:00
|
|
|
Value *LHSValue = Env.getValueStrict(LHS);
|
|
|
|
Value *RHSValue = Env.getValueStrict(RHS);
|
|
|
|
|
|
|
|
if (LHSValue == RHSValue)
|
|
|
|
return Env.getBoolLiteralValue(true);
|
|
|
|
|
|
|
|
if (auto *LHSBool = dyn_cast_or_null<BoolValue>(LHSValue))
|
|
|
|
if (auto *RHSBool = dyn_cast_or_null<BoolValue>(RHSValue))
|
|
|
|
return Env.makeIff(*LHSBool, *RHSBool);
|
2022-03-25 20:01:18 +00:00
|
|
|
|
|
|
|
return Env.makeAtomicBoolValue();
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:30:59 +02:00
|
|
|
// Functionally updates `V` such that any instances of `TopBool` are replaced
|
|
|
|
// with fresh atomic bools. Note: This implementation assumes that `B` is a
|
|
|
|
// tree; if `B` is a DAG, it will lose any sharing between subvalues that was
|
|
|
|
// present in the original .
|
|
|
|
static BoolValue &unpackValue(BoolValue &V, Environment &Env);
|
|
|
|
|
|
|
|
template <typename Derived, typename M>
|
|
|
|
BoolValue &unpackBinaryBoolValue(Environment &Env, BoolValue &B, M build) {
|
|
|
|
auto &V = *cast<Derived>(&B);
|
|
|
|
BoolValue &Left = V.getLeftSubValue();
|
|
|
|
BoolValue &Right = V.getRightSubValue();
|
|
|
|
BoolValue &ULeft = unpackValue(Left, Env);
|
|
|
|
BoolValue &URight = unpackValue(Right, Env);
|
|
|
|
|
|
|
|
if (&ULeft == &Left && &URight == &Right)
|
|
|
|
return V;
|
|
|
|
|
|
|
|
return (Env.*build)(ULeft, URight);
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:56:41 +00:00
|
|
|
static BoolValue &unpackValue(BoolValue &V, Environment &Env) {
|
2023-07-05 14:30:59 +02:00
|
|
|
switch (V.getKind()) {
|
|
|
|
case Value::Kind::Integer:
|
|
|
|
case Value::Kind::Reference:
|
|
|
|
case Value::Kind::Pointer:
|
|
|
|
case Value::Kind::Struct:
|
|
|
|
llvm_unreachable("BoolValue cannot have any of these kinds.");
|
|
|
|
|
|
|
|
case Value::Kind::AtomicBool:
|
|
|
|
return V;
|
|
|
|
|
|
|
|
case Value::Kind::TopBool:
|
|
|
|
// Unpack `TopBool` into a fresh atomic bool.
|
|
|
|
return Env.makeAtomicBoolValue();
|
|
|
|
|
|
|
|
case Value::Kind::Negation: {
|
|
|
|
auto &N = *cast<NegationValue>(&V);
|
|
|
|
BoolValue &Sub = N.getSubVal();
|
|
|
|
BoolValue &USub = unpackValue(Sub, Env);
|
|
|
|
|
|
|
|
if (&USub == &Sub)
|
|
|
|
return V;
|
|
|
|
return Env.makeNot(USub);
|
|
|
|
}
|
|
|
|
case Value::Kind::Conjunction:
|
|
|
|
return unpackBinaryBoolValue<ConjunctionValue>(Env, V,
|
|
|
|
&Environment::makeAnd);
|
|
|
|
case Value::Kind::Disjunction:
|
|
|
|
return unpackBinaryBoolValue<DisjunctionValue>(Env, V,
|
|
|
|
&Environment::makeOr);
|
|
|
|
case Value::Kind::Implication:
|
|
|
|
return unpackBinaryBoolValue<ImplicationValue>(
|
|
|
|
Env, V, &Environment::makeImplication);
|
|
|
|
case Value::Kind::Biconditional:
|
|
|
|
return unpackBinaryBoolValue<BiconditionalValue>(Env, V,
|
|
|
|
&Environment::makeIff);
|
2022-10-06 17:56:41 +00:00
|
|
|
}
|
2023-07-05 14:30:59 +02:00
|
|
|
llvm_unreachable("All reachable cases in switch return");
|
2022-10-06 17:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unpacks the value (if any) associated with `E` and updates `E` to the new
|
2023-01-03 15:41:38 +00:00
|
|
|
// value, if any unpacking occured. Also, does the lvalue-to-rvalue conversion,
|
|
|
|
// by skipping past the reference.
|
2022-10-06 17:56:41 +00:00
|
|
|
static Value *maybeUnpackLValueExpr(const Expr &E, Environment &Env) {
|
2023-05-22 06:17:17 +00:00
|
|
|
auto *Loc = Env.getStorageLocationStrict(E);
|
2022-10-06 17:56:41 +00:00
|
|
|
if (Loc == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
auto *Val = Env.getValue(*Loc);
|
|
|
|
|
|
|
|
auto *B = dyn_cast_or_null<BoolValue>(Val);
|
|
|
|
if (B == nullptr)
|
|
|
|
return Val;
|
|
|
|
|
|
|
|
auto &UnpackedVal = unpackValue(*B, Env);
|
|
|
|
if (&UnpackedVal == Val)
|
|
|
|
return Val;
|
|
|
|
Env.setValue(*Loc, UnpackedVal);
|
|
|
|
return &UnpackedVal;
|
|
|
|
}
|
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
static void propagateValue(const Expr &From, const Expr &To, Environment &Env) {
|
|
|
|
if (auto *Val = Env.getValueStrict(From))
|
|
|
|
Env.setValueStrict(To, *Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void propagateStorageLocation(const Expr &From, const Expr &To,
|
|
|
|
Environment &Env) {
|
|
|
|
if (auto *Loc = Env.getStorageLocationStrict(From))
|
|
|
|
Env.setStorageLocationStrict(To, *Loc);
|
|
|
|
}
|
|
|
|
|
2023-06-28 08:36:06 +00:00
|
|
|
// Propagates the value or storage location of `From` to `To` in cases where
|
2023-05-22 06:17:17 +00:00
|
|
|
// `From` may be either a glvalue or a prvalue. `To` must be a glvalue iff
|
|
|
|
// `From` is a glvalue.
|
|
|
|
static void propagateValueOrStorageLocation(const Expr &From, const Expr &To,
|
|
|
|
Environment &Env) {
|
|
|
|
assert(From.isGLValue() == To.isGLValue());
|
|
|
|
if (From.isGLValue())
|
|
|
|
propagateStorageLocation(From, To, Env);
|
|
|
|
else
|
|
|
|
propagateValue(From, To, Env);
|
|
|
|
}
|
|
|
|
|
2023-03-28 08:07:51 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-12-29 11:31:02 +00:00
|
|
|
class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
|
|
|
|
public:
|
2022-12-27 17:34:30 +00:00
|
|
|
TransferVisitor(const StmtToEnvMap &StmtToEnv, Environment &Env)
|
|
|
|
: StmtToEnv(StmtToEnv), Env(Env) {}
|
2021-12-29 11:31:02 +00:00
|
|
|
|
2022-01-04 13:47:14 +00:00
|
|
|
void VisitBinaryOperator(const BinaryOperator *S) {
|
2022-05-04 21:08:43 +00:00
|
|
|
const Expr *LHS = S->getLHS();
|
2022-03-11 11:52:53 +00:00
|
|
|
assert(LHS != nullptr);
|
|
|
|
|
2022-05-04 21:08:43 +00:00
|
|
|
const Expr *RHS = S->getRHS();
|
2022-03-11 11:52:53 +00:00
|
|
|
assert(RHS != nullptr);
|
|
|
|
|
2022-02-16 16:47:37 +00:00
|
|
|
switch (S->getOpcode()) {
|
|
|
|
case BO_Assign: {
|
2023-05-22 06:17:17 +00:00
|
|
|
auto *LHSLoc = Env.getStorageLocationStrict(*LHS);
|
2022-01-04 13:47:14 +00:00
|
|
|
if (LHSLoc == nullptr)
|
2022-02-16 16:47:37 +00:00
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
auto *RHSVal = Env.getValueStrict(*RHS);
|
2022-01-04 13:47:14 +00:00
|
|
|
if (RHSVal == nullptr)
|
2022-02-16 16:47:37 +00:00
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
|
|
|
|
// Assign a value to the storage location of the left-hand side.
|
|
|
|
Env.setValue(*LHSLoc, *RHSVal);
|
|
|
|
|
|
|
|
// Assign a storage location for the whole expression.
|
|
|
|
Env.setStorageLocation(*S, *LHSLoc);
|
2022-02-16 16:47:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BO_LAnd:
|
|
|
|
case BO_LOr: {
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
2023-03-23 07:45:40 +00:00
|
|
|
|
2023-05-25 09:22:37 +00:00
|
|
|
BoolValue &LHSVal = getLogicOperatorSubExprValue(*LHS);
|
|
|
|
BoolValue &RHSVal = getLogicOperatorSubExprValue(*RHS);
|
2023-03-23 07:45:40 +00:00
|
|
|
|
2022-02-16 16:47:37 +00:00
|
|
|
if (S->getOpcode() == BO_LAnd)
|
2023-05-25 09:22:37 +00:00
|
|
|
Env.setValue(Loc, Env.makeAnd(LHSVal, RHSVal));
|
2022-02-16 16:47:37 +00:00
|
|
|
else
|
2023-05-25 09:22:37 +00:00
|
|
|
Env.setValue(Loc, Env.makeOr(LHSVal, RHSVal));
|
2022-02-16 16:47:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-03-25 20:01:18 +00:00
|
|
|
case BO_NE:
|
|
|
|
case BO_EQ: {
|
|
|
|
auto &LHSEqRHSValue = evaluateBooleanEquality(*LHS, *RHS, Env);
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
|
|
|
Env.setValue(Loc, S->getOpcode() == BO_EQ ? LHSEqRHSValue
|
|
|
|
: Env.makeNot(LHSEqRHSValue));
|
|
|
|
break;
|
|
|
|
}
|
2022-06-16 21:57:28 +00:00
|
|
|
case BO_Comma: {
|
2023-05-17 13:27:35 +00:00
|
|
|
propagateValueOrStorageLocation(*RHS, *S, Env);
|
2022-06-16 21:57:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-16 16:47:37 +00:00
|
|
|
default:
|
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDeclRefExpr(const DeclRefExpr *S) {
|
2022-12-07 16:03:37 +00:00
|
|
|
const ValueDecl *VD = S->getDecl();
|
|
|
|
assert(VD != nullptr);
|
2023-06-29 06:39:39 +00:00
|
|
|
|
|
|
|
// `DeclRefExpr`s to fields and non-static methods aren't glvalues, and
|
|
|
|
// there's also no sensible `Value` we can assign to them, so skip them.
|
|
|
|
if (isa<FieldDecl>(VD))
|
|
|
|
return;
|
|
|
|
if (auto *Method = dyn_cast<CXXMethodDecl>(VD);
|
|
|
|
Method && !Method->isStatic())
|
|
|
|
return;
|
|
|
|
|
2023-05-08 06:38:42 +00:00
|
|
|
auto *DeclLoc = Env.getStorageLocation(*VD);
|
2022-01-04 13:47:14 +00:00
|
|
|
if (DeclLoc == nullptr)
|
|
|
|
return;
|
|
|
|
|
[clang][dataflow] Add `Strict` versions of `Value` and `StorageLocation` accessors.
This is part of the gradual migration to strict handling of value categories, as described in the RFC at https://discourse.llvm.org/t/70086.
This patch migrates some representative calls of the newly deprecated accessors to the new `Strict` functions. Followup patches will migrate the remaining callers. (There are a large number of callers, with some subtlety involved in some of them, so it makes sense to split this up into multiple patches rather than migrating all callers in one go.)
The `Strict` accessors as implemented here have some differences in semantics compared to the semantics originally proposed in the RFC; specifically:
* `setStorageLocationStrict()`: The RFC proposes to create an intermediate
`ReferenceValue` that then refers to the `StorageLocation` for the glvalue.
It turns out though that, even today, most places in the code are not doing
this but are instead associating glvalues directly with their
`StorageLocation`. It therefore didn't seem to make sense to introduce new
`ReferenceValue`s where there were none previously, so I have chosen to
instead make `setStorageLocationStrict()` simply call through to
`setStorageLocation(const Expr &, StorageLocation &)` and merely add the
assertion that the expression must be a glvalue.
* `getStorageLocationStrict()`: The RFC proposes that this should assert that
the storage location for the glvalue expression is associated with an
intermediate `ReferenceValue`, but, as explained, this is often not true.
The current state is inconsistent: Sometimes the intermediate
`ReferenceValue` is there, sometimes it isn't. For this reason,
`getStorageLocationStrict()` skips past a `ReferenceValue` if it is there but
otherwise directly returns the storage location associated with the
expression. This behavior is equivalent to the existing behavior of
`SkipPast::Reference`.
* `setValueStrict()`: The RFC proposes that this should always create the same
`StorageLocation` for a given `Value`, but, in fact, the transfer functions
that exist today don't guarantee this; almost all transfer functions
unconditionally create a new `StorageLocation` when associating an expression
with a `Value`.
There appears to be one special case:
`TerminatorVisitor::extendFlowCondition()` checks whether the expression is
already associated with a `StorageLocation` and, if so, reuses the existing
`StorageLocation` instead of creating a new one.
For this reason, `setValueStrict()` implements this logic (preserve an
existing `StorageLocation`) but makes no attempt to always associate the same
`StorageLocation` with a given `Value`, as nothing in the framework appers to
require this.
As `TerminatorVisitor::extendFlowCondition()` is an interesting special case,
the `setValue()` call there is among the ones that this patch migrates to
`setValueStrict()`.
Reviewed By: sammccall, ymandel, xazax.hun
Differential Revision: https://reviews.llvm.org/D150653
2023-05-17 09:12:46 +00:00
|
|
|
Env.setStorageLocationStrict(*S, *DeclLoc);
|
2022-01-04 13:47:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 11:31:02 +00:00
|
|
|
void VisitDeclStmt(const DeclStmt *S) {
|
2022-01-14 18:27:39 +00:00
|
|
|
// Group decls are converted into single decls in the CFG so the cast below
|
|
|
|
// is safe.
|
|
|
|
const auto &D = *cast<VarDecl>(S->getSingleDecl());
|
2022-02-23 13:38:51 +00:00
|
|
|
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
ProcessVarDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessVarDecl(const VarDecl &D) {
|
2022-02-23 13:38:51 +00:00
|
|
|
// Static local vars are already initialized in `Environment`.
|
|
|
|
if (D.hasGlobalStorage())
|
|
|
|
return;
|
|
|
|
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
if (D.getType()->isReferenceType()) {
|
|
|
|
// If this is the holding variable for a `BindingDecl`, we may already
|
|
|
|
// have a storage location set up -- so check. (See also explanation below
|
|
|
|
// where we process the `BindingDecl`.)
|
|
|
|
if (Env.getStorageLocation(D) == nullptr) {
|
|
|
|
const Expr *InitExpr = D.getInit();
|
|
|
|
assert(InitExpr != nullptr);
|
|
|
|
if (auto *InitExprLoc =
|
|
|
|
Env.getStorageLocation(*InitExpr, SkipPast::Reference)) {
|
|
|
|
Env.setStorageLocation(D, *InitExprLoc);
|
|
|
|
} else {
|
|
|
|
// Even though we have an initializer, we might not get an
|
|
|
|
// InitExprLoc, for example if the InitExpr is a CallExpr for which we
|
|
|
|
// don't have a function body. In this case, we just invent a storage
|
|
|
|
// location and value -- it's the best we can do.
|
|
|
|
StorageLocation &Loc =
|
|
|
|
Env.createStorageLocation(D.getType().getNonReferenceType());
|
|
|
|
Env.setStorageLocation(D, Loc);
|
|
|
|
if (Value *Val = Env.createValue(D.getType().getNonReferenceType()))
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not a reference type.
|
2022-01-14 18:27:39 +00:00
|
|
|
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
assert(Env.getStorageLocation(D) == nullptr);
|
|
|
|
StorageLocation &Loc = Env.createStorageLocation(D);
|
|
|
|
Env.setStorageLocation(D, Loc);
|
2022-01-14 18:27:39 +00:00
|
|
|
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
const Expr *InitExpr = D.getInit();
|
|
|
|
if (InitExpr == nullptr) {
|
|
|
|
// No initializer expression - associate `Loc` with a new value.
|
|
|
|
if (Value *Val = Env.createValue(D.getType()))
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
return;
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
2022-01-14 18:27:39 +00:00
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
if (auto *InitExprVal = Env.getValueStrict(*InitExpr))
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
Env.setValue(Loc, *InitExprVal);
|
|
|
|
|
|
|
|
if (Env.getValue(Loc) == nullptr) {
|
|
|
|
// We arrive here in (the few) cases where an expression is
|
|
|
|
// intentionally "uninterpreted". There are two ways to handle this
|
|
|
|
// situation: propagate the status, so that uninterpreted initializers
|
|
|
|
// result in uninterpreted variables, or provide a default value. We
|
|
|
|
// choose the latter so that later refinements of the variable can be
|
|
|
|
// used for reasoning about the surrounding code.
|
|
|
|
//
|
|
|
|
// FIXME. If and when we interpret all language cases, change this to
|
|
|
|
// assert that `InitExpr` is interpreted, rather than supplying a
|
|
|
|
// default value (assuming we don't update the environment API to return
|
|
|
|
// references).
|
|
|
|
if (Value *Val = Env.createValue(D.getType()))
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
}
|
2022-02-24 17:07:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 15:41:38 +00:00
|
|
|
// `DecompositionDecl` must be handled after we've interpreted the loc
|
|
|
|
// itself, because the binding expression refers back to the
|
|
|
|
// `DecompositionDecl` (even though it has no written name).
|
2022-02-24 17:07:41 +00:00
|
|
|
if (const auto *Decomp = dyn_cast<DecompositionDecl>(&D)) {
|
|
|
|
// If VarDecl is a DecompositionDecl, evaluate each of its bindings. This
|
|
|
|
// needs to be evaluated after initializing the values in the storage for
|
|
|
|
// VarDecl, as the bindings refer to them.
|
|
|
|
// FIXME: Add support for ArraySubscriptExpr.
|
2022-12-07 16:03:37 +00:00
|
|
|
// FIXME: Consider adding AST nodes used in BindingDecls to the CFG.
|
2022-02-24 17:07:41 +00:00
|
|
|
for (const auto *B : Decomp->bindings()) {
|
2022-12-07 16:03:37 +00:00
|
|
|
if (auto *ME = dyn_cast_or_null<MemberExpr>(B->getBinding())) {
|
|
|
|
auto *DE = dyn_cast_or_null<DeclRefExpr>(ME->getBase());
|
|
|
|
if (DE == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// ME and its base haven't been visited because they aren't included
|
|
|
|
// in the statements of the CFG basic block.
|
|
|
|
VisitDeclRefExpr(DE);
|
|
|
|
VisitMemberExpr(ME);
|
|
|
|
|
|
|
|
if (auto *Loc = Env.getStorageLocation(*ME, SkipPast::Reference))
|
|
|
|
Env.setStorageLocation(*B, *Loc);
|
|
|
|
} else if (auto *VD = B->getHoldingVar()) {
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
// Holding vars are used to back the `BindingDecl`s of tuple-like
|
|
|
|
// types. The holding var declarations appear after the
|
|
|
|
// `DecompositionDecl`, so we have to explicitly process them here
|
|
|
|
// to know their storage location. They will be processed a second
|
|
|
|
// time when we visit their `VarDecl`s, so we have code that protects
|
|
|
|
// against this above.
|
|
|
|
ProcessVarDecl(*VD);
|
|
|
|
auto *VDLoc = Env.getStorageLocation(*VD);
|
|
|
|
assert(VDLoc != nullptr);
|
|
|
|
Env.setStorageLocation(*B, *VDLoc);
|
2022-12-07 16:03:37 +00:00
|
|
|
}
|
2022-02-24 17:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 13:47:14 +00:00
|
|
|
void VisitImplicitCastExpr(const ImplicitCastExpr *S) {
|
2022-05-02 21:36:04 +00:00
|
|
|
const Expr *SubExpr = S->getSubExpr();
|
2022-01-13 13:53:52 +00:00
|
|
|
assert(SubExpr != nullptr);
|
|
|
|
|
|
|
|
switch (S->getCastKind()) {
|
2022-04-04 15:31:52 +00:00
|
|
|
case CK_IntegralToBoolean: {
|
|
|
|
// This cast creates a new, boolean value from the integral value. We
|
|
|
|
// model that with a fresh value in the environment, unless it's already a
|
|
|
|
// boolean.
|
[clang][dataflow] Add `Strict` versions of `Value` and `StorageLocation` accessors.
This is part of the gradual migration to strict handling of value categories, as described in the RFC at https://discourse.llvm.org/t/70086.
This patch migrates some representative calls of the newly deprecated accessors to the new `Strict` functions. Followup patches will migrate the remaining callers. (There are a large number of callers, with some subtlety involved in some of them, so it makes sense to split this up into multiple patches rather than migrating all callers in one go.)
The `Strict` accessors as implemented here have some differences in semantics compared to the semantics originally proposed in the RFC; specifically:
* `setStorageLocationStrict()`: The RFC proposes to create an intermediate
`ReferenceValue` that then refers to the `StorageLocation` for the glvalue.
It turns out though that, even today, most places in the code are not doing
this but are instead associating glvalues directly with their
`StorageLocation`. It therefore didn't seem to make sense to introduce new
`ReferenceValue`s where there were none previously, so I have chosen to
instead make `setStorageLocationStrict()` simply call through to
`setStorageLocation(const Expr &, StorageLocation &)` and merely add the
assertion that the expression must be a glvalue.
* `getStorageLocationStrict()`: The RFC proposes that this should assert that
the storage location for the glvalue expression is associated with an
intermediate `ReferenceValue`, but, as explained, this is often not true.
The current state is inconsistent: Sometimes the intermediate
`ReferenceValue` is there, sometimes it isn't. For this reason,
`getStorageLocationStrict()` skips past a `ReferenceValue` if it is there but
otherwise directly returns the storage location associated with the
expression. This behavior is equivalent to the existing behavior of
`SkipPast::Reference`.
* `setValueStrict()`: The RFC proposes that this should always create the same
`StorageLocation` for a given `Value`, but, in fact, the transfer functions
that exist today don't guarantee this; almost all transfer functions
unconditionally create a new `StorageLocation` when associating an expression
with a `Value`.
There appears to be one special case:
`TerminatorVisitor::extendFlowCondition()` checks whether the expression is
already associated with a `StorageLocation` and, if so, reuses the existing
`StorageLocation` instead of creating a new one.
For this reason, `setValueStrict()` implements this logic (preserve an
existing `StorageLocation`) but makes no attempt to always associate the same
`StorageLocation` with a given `Value`, as nothing in the framework appers to
require this.
As `TerminatorVisitor::extendFlowCondition()` is an interesting special case,
the `setValue()` call there is among the ones that this patch migrates to
`setValueStrict()`.
Reviewed By: sammccall, ymandel, xazax.hun
Differential Revision: https://reviews.llvm.org/D150653
2023-05-17 09:12:46 +00:00
|
|
|
if (auto *SubExprVal =
|
|
|
|
dyn_cast_or_null<BoolValue>(Env.getValueStrict(*SubExpr)))
|
|
|
|
Env.setValueStrict(*S, *SubExprVal);
|
2022-04-04 15:31:52 +00:00
|
|
|
else
|
|
|
|
// FIXME: If integer modeling is added, then update this code to create
|
|
|
|
// the boolean based on the integer model.
|
[clang][dataflow] Add `Strict` versions of `Value` and `StorageLocation` accessors.
This is part of the gradual migration to strict handling of value categories, as described in the RFC at https://discourse.llvm.org/t/70086.
This patch migrates some representative calls of the newly deprecated accessors to the new `Strict` functions. Followup patches will migrate the remaining callers. (There are a large number of callers, with some subtlety involved in some of them, so it makes sense to split this up into multiple patches rather than migrating all callers in one go.)
The `Strict` accessors as implemented here have some differences in semantics compared to the semantics originally proposed in the RFC; specifically:
* `setStorageLocationStrict()`: The RFC proposes to create an intermediate
`ReferenceValue` that then refers to the `StorageLocation` for the glvalue.
It turns out though that, even today, most places in the code are not doing
this but are instead associating glvalues directly with their
`StorageLocation`. It therefore didn't seem to make sense to introduce new
`ReferenceValue`s where there were none previously, so I have chosen to
instead make `setStorageLocationStrict()` simply call through to
`setStorageLocation(const Expr &, StorageLocation &)` and merely add the
assertion that the expression must be a glvalue.
* `getStorageLocationStrict()`: The RFC proposes that this should assert that
the storage location for the glvalue expression is associated with an
intermediate `ReferenceValue`, but, as explained, this is often not true.
The current state is inconsistent: Sometimes the intermediate
`ReferenceValue` is there, sometimes it isn't. For this reason,
`getStorageLocationStrict()` skips past a `ReferenceValue` if it is there but
otherwise directly returns the storage location associated with the
expression. This behavior is equivalent to the existing behavior of
`SkipPast::Reference`.
* `setValueStrict()`: The RFC proposes that this should always create the same
`StorageLocation` for a given `Value`, but, in fact, the transfer functions
that exist today don't guarantee this; almost all transfer functions
unconditionally create a new `StorageLocation` when associating an expression
with a `Value`.
There appears to be one special case:
`TerminatorVisitor::extendFlowCondition()` checks whether the expression is
already associated with a `StorageLocation` and, if so, reuses the existing
`StorageLocation` instead of creating a new one.
For this reason, `setValueStrict()` implements this logic (preserve an
existing `StorageLocation`) but makes no attempt to always associate the same
`StorageLocation` with a given `Value`, as nothing in the framework appers to
require this.
As `TerminatorVisitor::extendFlowCondition()` is an interesting special case,
the `setValue()` call there is among the ones that this patch migrates to
`setValueStrict()`.
Reviewed By: sammccall, ymandel, xazax.hun
Differential Revision: https://reviews.llvm.org/D150653
2023-05-17 09:12:46 +00:00
|
|
|
Env.setValueStrict(*S, Env.makeAtomicBoolValue());
|
2022-04-04 15:31:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-13 13:53:52 +00:00
|
|
|
case CK_LValueToRValue: {
|
2022-10-06 17:56:41 +00:00
|
|
|
// When an L-value is used as an R-value, it may result in sharing, so we
|
2023-01-03 15:41:38 +00:00
|
|
|
// need to unpack any nested `Top`s. We also need to strip off the
|
|
|
|
// `ReferenceValue` associated with the lvalue.
|
2022-10-06 17:56:41 +00:00
|
|
|
auto *SubExprVal = maybeUnpackLValueExpr(*SubExpr, Env);
|
2022-01-04 13:47:14 +00:00
|
|
|
if (SubExprVal == nullptr)
|
2022-01-13 13:53:52 +00:00
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
|
|
|
|
auto &ExprLoc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, ExprLoc);
|
|
|
|
Env.setValue(ExprLoc, *SubExprVal);
|
2022-01-13 13:53:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-04-04 15:31:52 +00:00
|
|
|
|
|
|
|
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.
|
2022-03-14 14:52:35 +00:00
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_ConstructorConversion:
|
|
|
|
case CK_UserDefinedConversion:
|
|
|
|
// FIXME: Add tests that excercise CK_UncheckedDerivedToBase,
|
|
|
|
// CK_ConstructorConversion, and CK_UserDefinedConversion.
|
2022-01-13 13:53:52 +00:00
|
|
|
case CK_NoOp: {
|
|
|
|
// FIXME: Consider making `Environment::getStorageLocation` skip noop
|
2023-05-17 13:27:35 +00:00
|
|
|
// expressions (this and other similar expressions in the file) instead
|
|
|
|
// of assigning them storage locations.
|
|
|
|
propagateValueOrStorageLocation(*SubExpr, *S, Env);
|
2022-01-13 13:53:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-06-29 06:39:39 +00:00
|
|
|
case CK_NullToPointer: {
|
2022-06-27 14:14:01 +02:00
|
|
|
auto &Loc = Env.createStorageLocation(S->getType());
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
|
|
|
|
|
|
|
auto &NullPointerVal =
|
|
|
|
Env.getOrCreateNullPointerValue(S->getType()->getPointeeType());
|
|
|
|
Env.setValue(Loc, NullPointerVal);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-29 06:39:39 +00:00
|
|
|
case CK_NullToMemberPointer:
|
|
|
|
// FIXME: Implement pointers to members. For now, don't associate a value
|
|
|
|
// with this expression.
|
|
|
|
break;
|
2023-07-05 07:46:52 +00:00
|
|
|
case CK_FunctionToPointerDecay: {
|
2023-04-18 04:49:38 +00:00
|
|
|
StorageLocation *PointeeLoc =
|
|
|
|
Env.getStorageLocation(*SubExpr, SkipPast::Reference);
|
|
|
|
if (PointeeLoc == nullptr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto &PointerLoc = Env.createStorageLocation(*S);
|
|
|
|
auto &PointerVal = Env.create<PointerValue>(*PointeeLoc);
|
|
|
|
Env.setStorageLocation(*S, PointerLoc);
|
|
|
|
Env.setValue(PointerLoc, PointerVal);
|
|
|
|
break;
|
|
|
|
}
|
2023-07-05 07:46:52 +00:00
|
|
|
case CK_BuiltinFnToFnPtr:
|
|
|
|
// Despite its name, the result type of `BuiltinFnToFnPtr` is a function,
|
|
|
|
// not a function pointer. In addition, builtin functions can only be
|
|
|
|
// called directly; it is not legal to take their address. We therefore
|
|
|
|
// don't need to create a value or storage location for them.
|
|
|
|
break;
|
2022-01-13 13:53:52 +00:00
|
|
|
default:
|
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitUnaryOperator(const UnaryOperator *S) {
|
2022-05-02 21:36:04 +00:00
|
|
|
const Expr *SubExpr = S->getSubExpr();
|
2022-01-17 16:23:24 +00:00
|
|
|
assert(SubExpr != nullptr);
|
|
|
|
|
|
|
|
switch (S->getOpcode()) {
|
|
|
|
case UO_Deref: {
|
2023-05-15 09:23:44 +00:00
|
|
|
const auto *SubExprVal =
|
[clang][dataflow] Add `Strict` versions of `Value` and `StorageLocation` accessors.
This is part of the gradual migration to strict handling of value categories, as described in the RFC at https://discourse.llvm.org/t/70086.
This patch migrates some representative calls of the newly deprecated accessors to the new `Strict` functions. Followup patches will migrate the remaining callers. (There are a large number of callers, with some subtlety involved in some of them, so it makes sense to split this up into multiple patches rather than migrating all callers in one go.)
The `Strict` accessors as implemented here have some differences in semantics compared to the semantics originally proposed in the RFC; specifically:
* `setStorageLocationStrict()`: The RFC proposes to create an intermediate
`ReferenceValue` that then refers to the `StorageLocation` for the glvalue.
It turns out though that, even today, most places in the code are not doing
this but are instead associating glvalues directly with their
`StorageLocation`. It therefore didn't seem to make sense to introduce new
`ReferenceValue`s where there were none previously, so I have chosen to
instead make `setStorageLocationStrict()` simply call through to
`setStorageLocation(const Expr &, StorageLocation &)` and merely add the
assertion that the expression must be a glvalue.
* `getStorageLocationStrict()`: The RFC proposes that this should assert that
the storage location for the glvalue expression is associated with an
intermediate `ReferenceValue`, but, as explained, this is often not true.
The current state is inconsistent: Sometimes the intermediate
`ReferenceValue` is there, sometimes it isn't. For this reason,
`getStorageLocationStrict()` skips past a `ReferenceValue` if it is there but
otherwise directly returns the storage location associated with the
expression. This behavior is equivalent to the existing behavior of
`SkipPast::Reference`.
* `setValueStrict()`: The RFC proposes that this should always create the same
`StorageLocation` for a given `Value`, but, in fact, the transfer functions
that exist today don't guarantee this; almost all transfer functions
unconditionally create a new `StorageLocation` when associating an expression
with a `Value`.
There appears to be one special case:
`TerminatorVisitor::extendFlowCondition()` checks whether the expression is
already associated with a `StorageLocation` and, if so, reuses the existing
`StorageLocation` instead of creating a new one.
For this reason, `setValueStrict()` implements this logic (preserve an
existing `StorageLocation`) but makes no attempt to always associate the same
`StorageLocation` with a given `Value`, as nothing in the framework appers to
require this.
As `TerminatorVisitor::extendFlowCondition()` is an interesting special case,
the `setValue()` call there is among the ones that this patch migrates to
`setValueStrict()`.
Reviewed By: sammccall, ymandel, xazax.hun
Differential Revision: https://reviews.llvm.org/D150653
2023-05-17 09:12:46 +00:00
|
|
|
cast_or_null<PointerValue>(Env.getValueStrict(*SubExpr));
|
2022-01-04 13:47:14 +00:00
|
|
|
if (SubExprVal == nullptr)
|
2022-01-17 16:23:24 +00:00
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
|
[clang][dataflow] Add `Strict` versions of `Value` and `StorageLocation` accessors.
This is part of the gradual migration to strict handling of value categories, as described in the RFC at https://discourse.llvm.org/t/70086.
This patch migrates some representative calls of the newly deprecated accessors to the new `Strict` functions. Followup patches will migrate the remaining callers. (There are a large number of callers, with some subtlety involved in some of them, so it makes sense to split this up into multiple patches rather than migrating all callers in one go.)
The `Strict` accessors as implemented here have some differences in semantics compared to the semantics originally proposed in the RFC; specifically:
* `setStorageLocationStrict()`: The RFC proposes to create an intermediate
`ReferenceValue` that then refers to the `StorageLocation` for the glvalue.
It turns out though that, even today, most places in the code are not doing
this but are instead associating glvalues directly with their
`StorageLocation`. It therefore didn't seem to make sense to introduce new
`ReferenceValue`s where there were none previously, so I have chosen to
instead make `setStorageLocationStrict()` simply call through to
`setStorageLocation(const Expr &, StorageLocation &)` and merely add the
assertion that the expression must be a glvalue.
* `getStorageLocationStrict()`: The RFC proposes that this should assert that
the storage location for the glvalue expression is associated with an
intermediate `ReferenceValue`, but, as explained, this is often not true.
The current state is inconsistent: Sometimes the intermediate
`ReferenceValue` is there, sometimes it isn't. For this reason,
`getStorageLocationStrict()` skips past a `ReferenceValue` if it is there but
otherwise directly returns the storage location associated with the
expression. This behavior is equivalent to the existing behavior of
`SkipPast::Reference`.
* `setValueStrict()`: The RFC proposes that this should always create the same
`StorageLocation` for a given `Value`, but, in fact, the transfer functions
that exist today don't guarantee this; almost all transfer functions
unconditionally create a new `StorageLocation` when associating an expression
with a `Value`.
There appears to be one special case:
`TerminatorVisitor::extendFlowCondition()` checks whether the expression is
already associated with a `StorageLocation` and, if so, reuses the existing
`StorageLocation` instead of creating a new one.
For this reason, `setValueStrict()` implements this logic (preserve an
existing `StorageLocation`) but makes no attempt to always associate the same
`StorageLocation` with a given `Value`, as nothing in the framework appers to
require this.
As `TerminatorVisitor::extendFlowCondition()` is an interesting special case,
the `setValue()` call there is among the ones that this patch migrates to
`setValueStrict()`.
Reviewed By: sammccall, ymandel, xazax.hun
Differential Revision: https://reviews.llvm.org/D150653
2023-05-17 09:12:46 +00:00
|
|
|
Env.setStorageLocationStrict(*S, SubExprVal->getPointeeLoc());
|
2022-01-17 16:23:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UO_AddrOf: {
|
2023-06-29 06:39:39 +00:00
|
|
|
// FIXME: Model pointers to members.
|
|
|
|
if (S->getType()->isMemberPointerType())
|
2022-01-17 16:23:24 +00:00
|
|
|
break;
|
|
|
|
|
2023-06-29 06:39:39 +00:00
|
|
|
if (StorageLocation *PointeeLoc = Env.getStorageLocationStrict(*SubExpr))
|
|
|
|
Env.setValueStrict(*S, Env.create<PointerValue>(*PointeeLoc));
|
2022-01-17 16:23:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-16 16:47:37 +00:00
|
|
|
case UO_LNot: {
|
|
|
|
auto *SubExprVal =
|
2023-05-22 06:17:17 +00:00
|
|
|
dyn_cast_or_null<BoolValue>(Env.getValueStrict(*SubExpr));
|
2022-02-16 16:47:37 +00:00
|
|
|
if (SubExprVal == nullptr)
|
2022-02-17 09:37:02 +00:00
|
|
|
break;
|
2022-02-16 16:47:37 +00:00
|
|
|
|
|
|
|
auto &ExprLoc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, ExprLoc);
|
2022-03-04 11:03:29 +00:00
|
|
|
Env.setValue(ExprLoc, Env.makeNot(*SubExprVal));
|
2022-02-17 09:37:02 +00:00
|
|
|
break;
|
2022-02-16 16:47:37 +00:00
|
|
|
}
|
2022-01-17 16:23:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
2022-01-04 13:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:15:53 +00:00
|
|
|
void VisitCXXThisExpr(const CXXThisExpr *S) {
|
|
|
|
auto *ThisPointeeLoc = Env.getThisPointeeStorageLocation();
|
2022-05-25 19:21:08 +00:00
|
|
|
if (ThisPointeeLoc == nullptr)
|
|
|
|
// Unions are not supported yet, and will not have a location for the
|
|
|
|
// `this` expression's pointee.
|
|
|
|
return;
|
2022-01-11 12:15:53 +00:00
|
|
|
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
2023-04-03 14:45:13 +00:00
|
|
|
Env.setValue(Loc, Env.create<PointerValue>(*ThisPointeeLoc));
|
2022-01-11 12:15:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 03:42:24 +00:00
|
|
|
void VisitCXXNewExpr(const CXXNewExpr *S) {
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
|
|
|
if (Value *Val = Env.createValue(S->getType()))
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
|
|
|
|
// Empty method.
|
|
|
|
// We consciously don't do anything on deletes. Diagnosing double deletes
|
|
|
|
// (for example) should be done by a specific analysis, not by the
|
|
|
|
// framework.
|
|
|
|
}
|
|
|
|
|
2022-08-04 17:42:01 +00:00
|
|
|
void VisitReturnStmt(const ReturnStmt *S) {
|
2023-05-02 00:08:30 +00:00
|
|
|
if (!Env.getDataflowAnalysisContext().getOptions().ContextSensitiveOpts)
|
2022-12-20 19:41:19 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-04 17:42:01 +00:00
|
|
|
auto *Ret = S->getRetValue();
|
|
|
|
if (Ret == nullptr)
|
|
|
|
return;
|
|
|
|
|
2023-05-23 09:35:52 +00:00
|
|
|
if (Ret->isPRValue()) {
|
|
|
|
auto *Val = Env.getValueStrict(*Ret);
|
|
|
|
if (Val == nullptr)
|
|
|
|
return;
|
2022-08-04 17:42:01 +00:00
|
|
|
|
2023-05-23 09:35:52 +00:00
|
|
|
// FIXME: Model NRVO.
|
|
|
|
Env.setReturnValue(Val);
|
|
|
|
} else {
|
|
|
|
auto *Loc = Env.getStorageLocationStrict(*Ret);
|
|
|
|
if (Loc == nullptr)
|
|
|
|
return;
|
2022-12-20 19:41:19 +00:00
|
|
|
|
2023-05-23 09:35:52 +00:00
|
|
|
// FIXME: Model NRVO.
|
|
|
|
Env.setReturnStorageLocation(Loc);
|
|
|
|
}
|
2022-08-04 17:42:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 12:15:53 +00:00
|
|
|
void VisitMemberExpr(const MemberExpr *S) {
|
|
|
|
ValueDecl *Member = S->getMemberDecl();
|
|
|
|
assert(Member != nullptr);
|
|
|
|
|
|
|
|
// FIXME: Consider assigning pointer values to function member expressions.
|
|
|
|
if (Member->isFunctionOrFunctionTemplate())
|
|
|
|
return;
|
|
|
|
|
2023-01-09 22:54:53 +00:00
|
|
|
// FIXME: if/when we add support for modeling enums, use that support here.
|
|
|
|
if (isa<EnumConstantDecl>(Member))
|
|
|
|
return;
|
|
|
|
|
2022-02-23 13:38:51 +00:00
|
|
|
if (auto *D = dyn_cast<VarDecl>(Member)) {
|
|
|
|
if (D->hasGlobalStorage()) {
|
2023-05-08 06:38:42 +00:00
|
|
|
auto *VarDeclLoc = Env.getStorageLocation(*D);
|
2022-02-23 13:38:51 +00:00
|
|
|
if (VarDeclLoc == nullptr)
|
|
|
|
return;
|
|
|
|
|
[clang][dataflow] Eliminate intermediate `ReferenceValue`s from `Environment::DeclToLoc`.
For the wider context of this change, see the RFC at
https://discourse.llvm.org/t/70086.
After this change, global and local variables of reference type are associated
directly with the `StorageLocation` of the referenced object instead of the
`StorageLocation` of a `ReferenceValue`.
Some tests that explicitly check for an existence of `ReferenceValue` for a
variable of reference type have been modified accordingly.
As discussed in the RFC, I have added an assertion to `Environment::join()` to
check that if both environments contain an entry for the same declaration in
`DeclToLoc`, they both map to the same `StorageLocation`. As discussed in
https://discourse.llvm.org/t/70086/5, this also necessitates removing
declarations from `DeclToLoc` when they go out of scope.
In the RFC, I proposed a gradual migration for this change, but it appears
that all of the callers of `Environment::setStorageLocation(const ValueDecl &,
SkipPast` are in the dataflow framework itself, and that there are only a few of
them.
As this is the function whose semantics are changing in a way that callers
potentially need to adapt to, I've decided to change the semantics of the
function directly.
The semantics of `getStorageLocation(const ValueDecl &, SkipPast SP` now no
longer depend on the behavior of the `SP` parameter. (There don't appear to be
any callers that use `SkipPast::ReferenceThenPointer`, so I've added an
assertion that forbids this usage.)
This patch adds a default argument for the `SP` parameter and removes the
explicit `SP` argument at the callsites that are touched by this change. A
followup patch will remove the argument from the remaining callsites,
allowing the `SkipPast` parameter to be removed entirely. (I don't want to do
that in this patch so that semantics-changing changes can be reviewed separately
from semantics-neutral changes.)
Reviewed By: ymandel, xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D149144
2023-05-04 07:42:05 +00:00
|
|
|
Env.setStorageLocation(*S, *VarDeclLoc);
|
2022-02-23 13:38:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 11:59:21 +00:00
|
|
|
AggregateStorageLocation *BaseLoc = getBaseObjectLocation(*S, Env);
|
2022-01-11 12:15:53 +00:00
|
|
|
if (BaseLoc == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto &MemberLoc = BaseLoc->getChild(*Member);
|
|
|
|
if (MemberLoc.getType()->isReferenceType()) {
|
2023-01-24 14:51:32 +00:00
|
|
|
// Based on its type, `MemberLoc` must be mapped either to nothing or to a
|
|
|
|
// `ReferenceValue`. For the former, we won't set a storage location for
|
|
|
|
// this expression, so as to maintain an invariant lvalue expressions;
|
|
|
|
// namely, that their location maps to a `ReferenceValue`. In this,
|
|
|
|
// lvalues are unlike other expressions, where it is valid for their
|
|
|
|
// location to map to nothing (because they are not modeled).
|
|
|
|
//
|
|
|
|
// Note: we need this invariant for lvalues so that, when accessing a
|
|
|
|
// value, we can distinguish an rvalue from an lvalue. An alternative
|
|
|
|
// design, which takes the expression's value category into account, would
|
|
|
|
// avoid the need for this invariant.
|
|
|
|
if (auto *V = Env.getValue(MemberLoc)) {
|
|
|
|
assert(isa<ReferenceValue>(V) &&
|
|
|
|
"reference-typed declarations map to `ReferenceValue`s");
|
|
|
|
Env.setStorageLocation(*S, MemberLoc);
|
|
|
|
}
|
2022-01-11 12:15:53 +00:00
|
|
|
} else {
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
2023-04-03 14:45:13 +00:00
|
|
|
Env.setValue(Loc, Env.create<ReferenceValue>(MemberLoc));
|
2022-01-11 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:28:59 +00:00
|
|
|
void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *S) {
|
|
|
|
const Expr *InitExpr = S->getExpr();
|
|
|
|
assert(InitExpr != nullptr);
|
2023-06-28 08:36:06 +00:00
|
|
|
propagateValueOrStorageLocation(*InitExpr, *S, Env);
|
2022-01-12 16:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 13:53:52 +00:00
|
|
|
void VisitCXXConstructExpr(const CXXConstructExpr *S) {
|
|
|
|
const CXXConstructorDecl *ConstructorDecl = S->getConstructor();
|
|
|
|
assert(ConstructorDecl != nullptr);
|
|
|
|
|
|
|
|
if (ConstructorDecl->isCopyOrMoveConstructor()) {
|
2023-02-22 10:36:39 +00:00
|
|
|
// It is permissible for a copy/move constructor to have additional
|
|
|
|
// parameters as long as they have default arguments defined for them.
|
|
|
|
assert(S->getNumArgs() != 0);
|
2022-01-13 13:53:52 +00:00
|
|
|
|
|
|
|
const Expr *Arg = S->getArg(0);
|
|
|
|
assert(Arg != nullptr);
|
|
|
|
|
2023-06-28 09:16:09 +00:00
|
|
|
auto *ArgLoc = cast_or_null<AggregateStorageLocation>(
|
2023-06-20 08:00:01 +00:00
|
|
|
Env.getStorageLocation(*Arg, SkipPast::Reference));
|
|
|
|
if (ArgLoc == nullptr)
|
|
|
|
return;
|
2022-01-13 13:53:52 +00:00
|
|
|
|
2023-06-20 08:00:01 +00:00
|
|
|
if (S->isElidable()) {
|
2022-01-13 13:53:52 +00:00
|
|
|
Env.setStorageLocation(*S, *ArgLoc);
|
2023-06-20 08:00:01 +00:00
|
|
|
} else {
|
|
|
|
auto &Loc =
|
|
|
|
cast<AggregateStorageLocation>(Env.createStorageLocation(*S));
|
2022-01-13 13:53:52 +00:00
|
|
|
Env.setStorageLocation(*S, Loc);
|
2023-06-20 08:00:01 +00:00
|
|
|
if (Value *Val = Env.createValue(S->getType())) {
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
copyRecord(*ArgLoc, Loc, Env);
|
|
|
|
}
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
2022-01-17 15:17:05 +00:00
|
|
|
if (Value *Val = Env.createValue(S->getType()))
|
|
|
|
Env.setValue(Loc, *Val);
|
2022-08-10 14:01:18 +00:00
|
|
|
|
|
|
|
transferInlineCall(S, ConstructorDecl);
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
|
|
|
|
if (S->getOperator() == OO_Equal) {
|
|
|
|
assert(S->getNumArgs() == 2);
|
|
|
|
|
|
|
|
const Expr *Arg0 = S->getArg(0);
|
|
|
|
assert(Arg0 != nullptr);
|
|
|
|
|
|
|
|
const Expr *Arg1 = S->getArg(1);
|
|
|
|
assert(Arg1 != nullptr);
|
|
|
|
|
|
|
|
// Evaluate only copy and move assignment operators.
|
2023-04-24 13:11:02 +00:00
|
|
|
const auto *Method =
|
|
|
|
dyn_cast_or_null<CXXMethodDecl>(S->getDirectCallee());
|
|
|
|
if (!Method)
|
|
|
|
return;
|
|
|
|
if (!Method->isCopyAssignmentOperator() &&
|
|
|
|
!Method->isMoveAssignmentOperator())
|
2022-01-13 13:53:52 +00:00
|
|
|
return;
|
|
|
|
|
2023-06-20 08:00:01 +00:00
|
|
|
auto *ObjectLoc = cast_or_null<AggregateStorageLocation>(
|
|
|
|
Env.getStorageLocation(*Arg0, SkipPast::Reference));
|
2022-01-13 13:53:52 +00:00
|
|
|
if (ObjectLoc == nullptr)
|
|
|
|
return;
|
|
|
|
|
2023-06-20 08:00:01 +00:00
|
|
|
auto *ArgLoc = cast_or_null<AggregateStorageLocation>(
|
|
|
|
Env.getStorageLocation(*Arg1, SkipPast::Reference));
|
|
|
|
if (ArgLoc == nullptr)
|
2022-01-13 13:53:52 +00:00
|
|
|
return;
|
|
|
|
|
2023-06-20 08:00:01 +00:00
|
|
|
copyRecord(*ArgLoc, *ObjectLoc, Env);
|
2022-03-16 22:20:05 +00:00
|
|
|
|
|
|
|
// FIXME: Add a test for the value of the whole expression.
|
|
|
|
// Assign a storage location for the whole expression.
|
|
|
|
Env.setStorageLocation(*S, *ObjectLoc);
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
|
|
|
|
if (S->getCastKind() == CK_ConstructorConversion) {
|
|
|
|
const Expr *SubExpr = S->getSubExpr();
|
|
|
|
assert(SubExpr != nullptr);
|
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
propagateValue(*SubExpr, *S, Env);
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
|
2022-01-17 15:17:05 +00:00
|
|
|
if (Value *Val = Env.createValue(S->getType()))
|
2023-05-22 06:17:17 +00:00
|
|
|
Env.setValueStrict(*S, *Val);
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCallExpr(const CallExpr *S) {
|
2022-04-01 12:51:23 +00:00
|
|
|
// Of clang's builtins, only `__builtin_expect` is handled explicitly, since
|
|
|
|
// others (like trap, debugtrap, and unreachable) are handled by CFG
|
|
|
|
// construction.
|
2022-01-13 13:53:52 +00:00
|
|
|
if (S->isCallToStdMove()) {
|
|
|
|
assert(S->getNumArgs() == 1);
|
|
|
|
|
|
|
|
const Expr *Arg = S->getArg(0);
|
|
|
|
assert(Arg != nullptr);
|
|
|
|
|
|
|
|
auto *ArgLoc = Env.getStorageLocation(*Arg, SkipPast::None);
|
|
|
|
if (ArgLoc == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Env.setStorageLocation(*S, *ArgLoc);
|
2022-04-01 12:51:23 +00:00
|
|
|
} else if (S->getDirectCallee() != nullptr &&
|
|
|
|
S->getDirectCallee()->getBuiltinID() ==
|
|
|
|
Builtin::BI__builtin_expect) {
|
|
|
|
assert(S->getNumArgs() > 0);
|
|
|
|
assert(S->getArg(0) != nullptr);
|
|
|
|
// `__builtin_expect` returns by-value, so strip away any potential
|
|
|
|
// references in the argument.
|
2022-05-04 21:08:43 +00:00
|
|
|
auto *ArgLoc = Env.getStorageLocation(*S->getArg(0), SkipPast::Reference);
|
2022-04-01 12:51:23 +00:00
|
|
|
if (ArgLoc == nullptr)
|
|
|
|
return;
|
|
|
|
Env.setStorageLocation(*S, *ArgLoc);
|
2022-07-26 17:54:13 +00:00
|
|
|
} else if (const FunctionDecl *F = S->getDirectCallee()) {
|
2022-08-10 14:01:18 +00:00
|
|
|
transferInlineCall(S, F);
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *S) {
|
|
|
|
const Expr *SubExpr = S->getSubExpr();
|
|
|
|
assert(SubExpr != nullptr);
|
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
Value *SubExprVal = Env.getValueStrict(*SubExpr);
|
|
|
|
if (SubExprVal == nullptr)
|
2022-01-13 13:53:52 +00:00
|
|
|
return;
|
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocationStrict(*S, Loc);
|
|
|
|
Env.setValue(Loc, *SubExprVal);
|
2022-01-13 13:53:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 18:27:39 +00:00
|
|
|
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
|
|
|
|
const Expr *SubExpr = S->getSubExpr();
|
|
|
|
assert(SubExpr != nullptr);
|
2022-01-04 13:47:14 +00:00
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
propagateValue(*SubExpr, *S, Env);
|
2022-01-14 18:27:39 +00:00
|
|
|
}
|
2022-01-13 13:53:52 +00:00
|
|
|
|
2022-01-14 18:27:39 +00:00
|
|
|
void VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
|
|
|
|
if (S->getCastKind() == CK_NoOp) {
|
|
|
|
const Expr *SubExpr = S->getSubExpr();
|
|
|
|
assert(SubExpr != nullptr);
|
2022-01-04 13:47:14 +00:00
|
|
|
|
2023-05-22 06:17:17 +00:00
|
|
|
propagateValueOrStorageLocation(*SubExpr, *S, Env);
|
2022-01-04 13:47:14 +00:00
|
|
|
}
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:56:21 +00:00
|
|
|
void VisitConditionalOperator(const ConditionalOperator *S) {
|
|
|
|
// FIXME: Revisit this once flow conditions are added to the framework. For
|
|
|
|
// `a = b ? c : d` we can add `b => a == c && !b => a == d` to the flow
|
|
|
|
// condition.
|
2023-05-22 06:17:17 +00:00
|
|
|
if (S->isGLValue()) {
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocationStrict(*S, Loc);
|
|
|
|
if (Value *Val = Env.createValue(S->getType()))
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
} else if (Value *Val = Env.createValue(S->getType())) {
|
|
|
|
Env.setValueStrict(*S, *Val);
|
|
|
|
}
|
2022-01-19 13:56:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 16:17:22 +00:00
|
|
|
void VisitInitListExpr(const InitListExpr *S) {
|
|
|
|
QualType Type = S->getType();
|
|
|
|
|
|
|
|
auto &Loc = Env.createStorageLocation(*S);
|
|
|
|
Env.setStorageLocation(*S, Loc);
|
|
|
|
|
|
|
|
auto *Val = Env.createValue(Type);
|
|
|
|
if (Val == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Env.setValue(Loc, *Val);
|
|
|
|
|
|
|
|
if (Type->isStructureOrClassType()) {
|
2023-02-27 18:15:39 +00:00
|
|
|
// Unnamed bitfields are only used for padding and are not appearing in
|
|
|
|
// `InitListExpr`'s inits. However, those fields do appear in RecordDecl's
|
|
|
|
// field list, and we thus need to remove them before mapping inits to
|
|
|
|
// fields to avoid mapping inits to the wrongs fields.
|
|
|
|
std::vector<FieldDecl *> Fields;
|
|
|
|
llvm::copy_if(
|
|
|
|
Type->getAsRecordDecl()->fields(), std::back_inserter(Fields),
|
|
|
|
[](const FieldDecl *Field) { return !Field->isUnnamedBitfield(); });
|
|
|
|
for (auto It : llvm::zip(Fields, S->inits())) {
|
2022-07-25 20:23:23 +02:00
|
|
|
const FieldDecl *Field = std::get<0>(It);
|
2022-01-24 16:17:22 +00:00
|
|
|
assert(Field != nullptr);
|
|
|
|
|
2022-07-25 20:23:23 +02:00
|
|
|
const Expr *Init = std::get<1>(It);
|
2022-01-24 16:17:22 +00:00
|
|
|
assert(Init != nullptr);
|
|
|
|
|
|
|
|
if (Value *InitVal = Env.getValue(*Init, SkipPast::None))
|
|
|
|
cast<StructValue>(Val)->setChild(*Field, *InitVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME: Implement array initialization.
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:10:38 +00:00
|
|
|
void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
|
2023-05-22 06:17:17 +00:00
|
|
|
Env.setValueStrict(*S, Env.getBoolLiteralValue(S->getValue()));
|
2022-01-26 12:10:38 +00:00
|
|
|
}
|
2022-01-14 18:27:39 +00:00
|
|
|
|
2023-06-15 19:07:05 +00:00
|
|
|
void VisitIntegerLiteral(const IntegerLiteral *S) {
|
|
|
|
Env.setValueStrict(*S, Env.getIntLiteralValue(S->getValue()));
|
|
|
|
}
|
|
|
|
|
2022-05-04 21:08:43 +00:00
|
|
|
void VisitParenExpr(const ParenExpr *S) {
|
|
|
|
// The CFG does not contain `ParenExpr` as top-level statements in basic
|
|
|
|
// blocks, however manual traversal to sub-expressions may encounter them.
|
|
|
|
// Redirect to the sub-expression.
|
|
|
|
auto *SubExpr = S->getSubExpr();
|
|
|
|
assert(SubExpr != nullptr);
|
|
|
|
Visit(SubExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitExprWithCleanups(const ExprWithCleanups *S) {
|
|
|
|
// The CFG does not contain `ExprWithCleanups` as top-level statements in
|
|
|
|
// basic blocks, however manual traversal to sub-expressions may encounter
|
|
|
|
// them. Redirect to the sub-expression.
|
|
|
|
auto *SubExpr = S->getSubExpr();
|
|
|
|
assert(SubExpr != nullptr);
|
|
|
|
Visit(SubExpr);
|
|
|
|
}
|
|
|
|
|
2022-01-14 18:27:39 +00:00
|
|
|
private:
|
2023-05-25 09:22:37 +00:00
|
|
|
/// Returns the value for the sub-expression `SubExpr` of a logic operator.
|
|
|
|
BoolValue &getLogicOperatorSubExprValue(const Expr &SubExpr) {
|
2022-03-11 11:52:53 +00:00
|
|
|
// `SubExpr` and its parent logic operator might be part of different basic
|
|
|
|
// blocks. We try to access the value that is assigned to `SubExpr` in the
|
|
|
|
// corresponding environment.
|
2023-05-25 09:22:37 +00:00
|
|
|
if (const Environment *SubExprEnv = StmtToEnv.getEnvironment(SubExpr))
|
|
|
|
if (auto *Val =
|
|
|
|
dyn_cast_or_null<BoolValue>(SubExprEnv->getValueStrict(SubExpr)))
|
|
|
|
return *Val;
|
|
|
|
|
|
|
|
// The sub-expression may lie within a basic block that isn't reachable,
|
|
|
|
// even if we need it to evaluate the current (reachable) expression
|
|
|
|
// (see https://discourse.llvm.org/t/70775). In this case, visit `SubExpr`
|
|
|
|
// within the current environment and then try to get the value that gets
|
|
|
|
// assigned to it.
|
|
|
|
if (Env.getValueStrict(SubExpr) == nullptr)
|
2022-05-17 18:08:25 +00:00
|
|
|
Visit(&SubExpr);
|
2023-05-22 06:17:17 +00:00
|
|
|
if (auto *Val = dyn_cast_or_null<BoolValue>(Env.getValueStrict(SubExpr)))
|
2023-05-25 09:22:37 +00:00
|
|
|
return *Val;
|
2022-03-11 11:52:53 +00:00
|
|
|
|
|
|
|
// If the value of `SubExpr` is still unknown, we create a fresh symbolic
|
|
|
|
// boolean value for it.
|
2023-05-25 09:22:37 +00:00
|
|
|
return Env.makeAtomicBoolValue();
|
2022-03-11 11:52:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:01:18 +00:00
|
|
|
// If context sensitivity is enabled, try to analyze the body of the callee
|
|
|
|
// `F` of `S`. The type `E` must be either `CallExpr` or `CXXConstructExpr`.
|
|
|
|
template <typename E>
|
|
|
|
void transferInlineCall(const E *S, const FunctionDecl *F) {
|
2023-05-02 00:08:30 +00:00
|
|
|
const auto &Options = Env.getDataflowAnalysisContext().getOptions();
|
2022-08-15 19:58:23 +00:00
|
|
|
if (!(Options.ContextSensitiveOpts &&
|
|
|
|
Env.canDescend(Options.ContextSensitiveOpts->Depth, F)))
|
2022-08-10 14:01:18 +00:00
|
|
|
return;
|
|
|
|
|
2023-05-02 00:08:30 +00:00
|
|
|
const ControlFlowContext *CFCtx =
|
|
|
|
Env.getDataflowAnalysisContext().getControlFlowContext(F);
|
2022-08-10 14:01:18 +00:00
|
|
|
if (!CFCtx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: We don't support context-sensitive analysis of recursion, so
|
|
|
|
// we should return early here if `F` is the same as the `FunctionDecl`
|
|
|
|
// holding `S` itself.
|
|
|
|
|
|
|
|
auto ExitBlock = CFCtx->getCFG().getExit().getBlockID();
|
|
|
|
|
|
|
|
auto CalleeEnv = Env.pushCall(S);
|
|
|
|
|
|
|
|
// FIXME: Use the same analysis as the caller for the callee. Note,
|
|
|
|
// though, that doing so would require support for changing the analysis's
|
|
|
|
// ASTContext.
|
|
|
|
assert(CFCtx->getDecl() != nullptr &&
|
|
|
|
"ControlFlowContexts in the environment should always carry a decl");
|
|
|
|
auto Analysis = NoopAnalysis(CFCtx->getDecl()->getASTContext(),
|
2022-08-15 19:58:23 +00:00
|
|
|
DataflowAnalysisOptions{Options});
|
2022-08-10 14:01:18 +00:00
|
|
|
|
|
|
|
auto BlockToOutputState =
|
|
|
|
dataflow::runDataflowAnalysis(*CFCtx, Analysis, CalleeEnv);
|
|
|
|
assert(BlockToOutputState);
|
|
|
|
assert(ExitBlock < BlockToOutputState->size());
|
|
|
|
|
2023-06-24 02:45:17 +02:00
|
|
|
auto &ExitState = (*BlockToOutputState)[ExitBlock];
|
2022-08-10 14:01:18 +00:00
|
|
|
assert(ExitState);
|
|
|
|
|
2023-05-23 09:35:52 +00:00
|
|
|
Env.popCall(S, ExitState->Env);
|
2022-08-10 14:01:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 16:47:37 +00:00
|
|
|
const StmtToEnvMap &StmtToEnv;
|
2021-12-29 11:31:02 +00:00
|
|
|
Environment &Env;
|
|
|
|
};
|
|
|
|
|
2023-03-28 08:07:51 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-12-27 17:34:30 +00:00
|
|
|
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env) {
|
|
|
|
TransferVisitor(StmtToEnv, Env).Visit(&S);
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dataflow
|
|
|
|
} // namespace clang
|