Skip to content

Commit 9822f60

Browse files
Googlercopybara-github
authored andcommitted
Ensure raw pointers have nullability in a central location (extending ensureRawPointerHasValue)
Remove some copies of code that ensured the nullability scattered through several transfer functions. PiperOrigin-RevId: 696973306 Change-Id: Iee9d8a0dfe4ce664828f5848cdb2dedbb33d30ba
1 parent fac88ac commit 9822f60

File tree

1 file changed

+20
-47
lines changed

1 file changed

+20
-47
lines changed

nullability/pointer_nullability_analysis.cc

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,6 @@ absl::Nullable<PointerValue *> ensureRawPointerHasValue(
106106
return Val;
107107
}
108108

109-
// If `Elt` is an expression of raw pointer type, ensures that it has a
110-
// `PointerValue` associated with it.
111-
void ensureRawPointerHasValue(const CFGElement &Elt, Environment &Env) {
112-
auto S = Elt.getAs<CFGStmt>();
113-
if (!S) return;
114-
115-
const Expr *E = dyn_cast<Expr>(S->getStmt());
116-
if (!E) return;
117-
118-
ensureRawPointerHasValue(E, Env);
119-
}
120-
121109
void computeNullability(absl::Nonnull<const Expr *> E,
122110
TransferState<PointerNullabilityLattice> &State,
123111
std::function<TypeNullability()> Compute) {
@@ -330,6 +318,25 @@ void initPointerFromTypeNullability(
330318
getPointerTypeNullability(E, State.Lattice));
331319
}
332320

321+
// If `Elt` is an expression of raw pointer type, ensures that it has a
322+
// `PointerValue` associated with it. Also ensure that it has nullability
323+
// state.
324+
void ensureRawPointerHasValueAndNullability(
325+
const CFGElement &Elt, Environment &Env,
326+
TransferState<PointerNullabilityLattice> &State) {
327+
auto S = Elt.getAs<CFGStmt>();
328+
if (!S) return;
329+
330+
const Expr *E = dyn_cast<Expr>(S->getStmt());
331+
if (!E) return;
332+
333+
if (auto *PointerVal = ensureRawPointerHasValue(E, Env)) {
334+
if (!hasPointerNullState(*PointerVal)) {
335+
initPointerFromTypeNullability(*PointerVal, E, State);
336+
}
337+
}
338+
}
339+
333340
/// If the pointer value stored at `PointerLoc` has any "top" nullability
334341
/// properties, creates a new pointer value referencing the same location with
335342
/// the "top" properties unpacked into fresh atoms. Returns:
@@ -570,12 +577,6 @@ void transferValue_SmartPointerReleaseCall(
570577
if (MCE->getType()->getCanonicalTypeUnqualified() !=
571578
underlyingRawPointerType(MCE->getObjectType())
572579
->getCanonicalTypeUnqualified()) {
573-
if (isSupportedRawPointerType(MCE->getType())) {
574-
// If the result is a raw pointer (usually the case for `release()`),
575-
// ensure the nullability is initialized.
576-
if (auto *PointerVal = ensureRawPointerHasValue(MCE, State.Env))
577-
initPointerFromTypeNullability(*PointerVal, MCE, State);
578-
}
579580
return;
580581
}
581582

@@ -641,12 +642,6 @@ void transferValue_SmartPointerGetCall(
641642
if (MCE->getType()->getCanonicalTypeUnqualified() !=
642643
underlyingRawPointerType(MCE->getObjectType())
643644
->getCanonicalTypeUnqualified()) {
644-
if (isSupportedRawPointerType(MCE->getType())) {
645-
// If the result is a raw pointer (usually the case for `get()`), ensure
646-
// the nullability is initialized.
647-
if (auto *PointerVal = ensureRawPointerHasValue(MCE, State.Env))
648-
initPointerFromTypeNullability(*PointerVal, MCE, State);
649-
}
650645
return;
651646
}
652647
if (Value *Val = getPointerValueFromSmartPointer(
@@ -691,12 +686,6 @@ void transferValue_SmartPointerOperatorStar(
691686
underlyingRawPointerType(getReceiverIgnoringImpCastsType(OpCall))
692687
->getPointeeType()
693688
->getCanonicalTypeUnqualified()) {
694-
if (isSupportedRawPointerType(ReturnType)) {
695-
// If the result is a raw pointer (unusual for `operator*()` but
696-
// possible), ensure the nullability is initialized.
697-
if (auto *PointerVal = ensureRawPointerHasValue(OpCall, State.Env))
698-
initPointerFromTypeNullability(*PointerVal, OpCall, State);
699-
}
700689
return;
701690
}
702691
if (PointerValue *Val = getSmartPointerValue(OpCall->getArg(0), State.Env)) {
@@ -712,12 +701,6 @@ void transferValue_SmartPointerOperatorArrow(
712701
if (OpCall->getType()->getCanonicalTypeUnqualified() !=
713702
underlyingRawPointerType(getReceiverIgnoringImpCastsType(OpCall))
714703
->getCanonicalTypeUnqualified()) {
715-
if (isSupportedRawPointerType(OpCall->getType())) {
716-
// If the result is a raw pointer (usually the case for `operator->`),
717-
// ensure the nullability is initialized.
718-
if (auto *PointerVal = ensureRawPointerHasValue(OpCall, State.Env))
719-
initPointerFromTypeNullability(*PointerVal, OpCall, State);
720-
}
721704
return;
722705
}
723706
if (PointerValue *Val = getSmartPointerValue(OpCall->getArg(0), State.Env)) {
@@ -911,16 +894,6 @@ void transferValue_SmartPointerArrowMemberExpr(
911894

912895
initPointerNullState(*PtrVal, State.Env.getDataflowAnalysisContext(),
913896
Nullability);
914-
915-
// If the result of the arrow access is a pointer, we also need to ensure its
916-
// nullability is initialized. Another transfer function may handle this, but
917-
// in cases where there's no e.g. ImplicitCastExpr or similar wrapping the
918-
// expression, it may be missing null state.
919-
if (ME->getType()->isPointerType()) {
920-
auto *ExprPtrVal = getPointerValue(ME, State.Env);
921-
CHECK(ExprPtrVal != nullptr);
922-
initPointerFromTypeNullability(*ExprPtrVal, ME, State);
923-
}
924897
}
925898

926899
void transferValue_Pointer(absl::Nonnull<const Expr *> PointerExpr,
@@ -1939,7 +1912,7 @@ void PointerNullabilityAnalysis::transfer(const CFGElement &Elt,
19391912

19401913
TypeTransferer(Elt, getASTContext(), State);
19411914
ValueTransferer(Elt, getASTContext(), State);
1942-
ensureRawPointerHasValue(Elt, Env);
1915+
ensureRawPointerHasValueAndNullability(Elt, Env, State);
19431916
ensureSmartPointerInitialized(Elt, State);
19441917
}
19451918

0 commit comments

Comments
 (0)