Skip to content

Commit dd33108

Browse files
authored
[analyzer][NFC] Factor out SymbolManager::get<*> (#121781)
Replace the family of `SymbolManager::get*Symbol(...)` member functions with a single generic `SymbolManager::get<*>` member function.
1 parent e8cc4d2 commit dd33108

File tree

7 files changed

+58
-228
lines changed

7 files changed

+58
-228
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ class SymbolConjured : public SymbolData {
113113

114114
void dumpToStream(raw_ostream &os) const override;
115115

116-
static void Profile(llvm::FoldingSetNodeID& profile, const Stmt *S,
117-
QualType T, unsigned Count, const LocationContext *LCtx,
116+
static void Profile(llvm::FoldingSetNodeID &profile, const Stmt *S,
117+
const LocationContext *LCtx, QualType T, unsigned Count,
118118
const void *SymbolTag) {
119-
profile.AddInteger((unsigned) SymbolConjuredKind);
119+
profile.AddInteger((unsigned)SymbolConjuredKind);
120120
profile.AddPointer(S);
121121
profile.AddPointer(LCtx);
122122
profile.Add(T);
@@ -125,7 +125,7 @@ class SymbolConjured : public SymbolData {
125125
}
126126

127127
void Profile(llvm::FoldingSetNodeID& profile) override {
128-
Profile(profile, S, T, Count, LCtx, SymbolTag);
128+
Profile(profile, S, LCtx, T, Count, SymbolTag);
129129
}
130130

131131
// Implement isa<T> support.
@@ -224,6 +224,8 @@ class SymbolMetadata : public SymbolData {
224224
const Stmt *S;
225225
QualType T;
226226
const LocationContext *LCtx;
227+
/// Count can be used to differentiate regions corresponding to
228+
/// different loop iterations, thus, making the symbol path-dependent.
227229
unsigned Count;
228230
const void *Tag;
229231

@@ -525,14 +527,18 @@ class SymbolManager {
525527

526528
static bool canSymbolicate(QualType T);
527529

528-
/// Make a unique symbol for MemRegion R according to its kind.
529-
const SymbolRegionValue* getRegionValueSymbol(const TypedValueRegion* R);
530+
/// Create or retrieve a SymExpr of type \p SymExprT for the given arguments.
531+
/// Use the arguments to check for an existing SymExpr and return it,
532+
/// otherwise, create a new one and keep a pointer to it to avoid duplicates.
533+
template <typename SymExprT, typename... Args>
534+
const SymExprT *acquire(Args &&...args);
530535

531-
const SymbolConjured* conjureSymbol(const Stmt *E,
532-
const LocationContext *LCtx,
533-
QualType T,
536+
const SymbolConjured *conjureSymbol(const Stmt *E,
537+
const LocationContext *LCtx, QualType T,
534538
unsigned VisitCount,
535-
const void *SymbolTag = nullptr);
539+
const void *SymbolTag = nullptr) {
540+
return acquire<SymbolConjured>(E, LCtx, T, VisitCount, SymbolTag);
541+
}
536542

537543
const SymbolConjured* conjureSymbol(const Expr *E,
538544
const LocationContext *LCtx,
@@ -541,41 +547,6 @@ class SymbolManager {
541547
return conjureSymbol(E, LCtx, E->getType(), VisitCount, SymbolTag);
542548
}
543549

544-
const SymbolDerived *getDerivedSymbol(SymbolRef parentSymbol,
545-
const TypedValueRegion *R);
546-
547-
const SymbolExtent *getExtentSymbol(const SubRegion *R);
548-
549-
/// Creates a metadata symbol associated with a specific region.
550-
///
551-
/// VisitCount can be used to differentiate regions corresponding to
552-
/// different loop iterations, thus, making the symbol path-dependent.
553-
const SymbolMetadata *getMetadataSymbol(const MemRegion *R, const Stmt *S,
554-
QualType T,
555-
const LocationContext *LCtx,
556-
unsigned VisitCount,
557-
const void *SymbolTag = nullptr);
558-
559-
const SymbolCast* getCastSymbol(const SymExpr *Operand,
560-
QualType From, QualType To);
561-
562-
const SymIntExpr *getSymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
563-
APSIntPtr rhs, QualType t);
564-
565-
const SymIntExpr *getSymIntExpr(const SymExpr &lhs, BinaryOperator::Opcode op,
566-
APSIntPtr rhs, QualType t) {
567-
return getSymIntExpr(&lhs, op, rhs, t);
568-
}
569-
570-
const IntSymExpr *getIntSymExpr(APSIntPtr lhs, BinaryOperator::Opcode op,
571-
const SymExpr *rhs, QualType t);
572-
573-
const SymSymExpr *getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
574-
const SymExpr *rhs, QualType t);
575-
576-
const UnarySymExpr *getUnarySymExpr(const SymExpr *operand,
577-
UnaryOperator::Opcode op, QualType t);
578-
579550
QualType getType(const SymExpr *SE) const {
580551
return SE->getType();
581552
}
@@ -707,6 +678,19 @@ class SymbolVisitor {
707678
virtual bool VisitMemRegion(const MemRegion *) { return true; }
708679
};
709680

681+
template <typename T, typename... Args>
682+
const T *SymbolManager::acquire(Args &&...args) {
683+
llvm::FoldingSetNodeID profile;
684+
T::Profile(profile, args...);
685+
void *InsertPos;
686+
SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
687+
if (!SD) {
688+
SD = Alloc.make<T>(std::forward<Args>(args)...);
689+
DataSet.InsertNode(SD, InsertPos);
690+
}
691+
return cast<T>(SD);
692+
}
693+
710694
} // namespace ento
711695

712696
} // namespace clang

clang/lib/StaticAnalyzer/Core/MemRegion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
811811
switch (SR->getKind()) {
812812
case MemRegion::AllocaRegionKind:
813813
case MemRegion::SymbolicRegionKind:
814-
return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
814+
return nonloc::SymbolVal(SymMgr.acquire<SymbolExtent>(SR));
815815
case MemRegion::StringRegionKind:
816816
return SVB.makeIntVal(
817817
cast<StringRegion>(SR)->getStringLiteral()->getByteLength() + 1,
@@ -829,7 +829,7 @@ DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
829829
case MemRegion::ObjCStringRegionKind: {
830830
QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx);
831831
if (isa<VariableArrayType>(Ty))
832-
return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
832+
return nonloc::SymbolVal(SymMgr.acquire<SymbolExtent>(SR));
833833

834834
if (Ty->isIncompleteType())
835835
return UnknownVal();
@@ -897,7 +897,7 @@ DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
897897
case MemRegion::BlockDataRegionKind:
898898
case MemRegion::BlockCodeRegionKind:
899899
case MemRegion::FunctionCodeRegionKind:
900-
return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
900+
return nonloc::SymbolVal(SymMgr.acquire<SymbolExtent>(SR));
901901
default:
902902
llvm_unreachable("Unhandled region");
903903
}

clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ class SymbolicRangeInferrer
14711471
return getRangeForNegatedExpr(
14721472
[SSE, State = this->State]() -> SymbolRef {
14731473
if (SSE->getOpcode() == BO_Sub)
1474-
return State->getSymbolManager().getSymSymExpr(
1474+
return State->getSymbolManager().acquire<SymSymExpr>(
14751475
SSE->getRHS(), BO_Sub, SSE->getLHS(), SSE->getType());
14761476
return nullptr;
14771477
},
@@ -1481,8 +1481,8 @@ class SymbolicRangeInferrer
14811481
std::optional<RangeSet> getRangeForNegatedSym(SymbolRef Sym) {
14821482
return getRangeForNegatedExpr(
14831483
[Sym, State = this->State]() {
1484-
return State->getSymbolManager().getUnarySymExpr(Sym, UO_Minus,
1485-
Sym->getType());
1484+
return State->getSymbolManager().acquire<UnarySymExpr>(
1485+
Sym, UO_Minus, Sym->getType());
14861486
},
14871487
Sym->getType());
14881488
}
@@ -1495,7 +1495,7 @@ class SymbolicRangeInferrer
14951495
if (!IsCommutative)
14961496
return std::nullopt;
14971497

1498-
SymbolRef Commuted = State->getSymbolManager().getSymSymExpr(
1498+
SymbolRef Commuted = State->getSymbolManager().acquire<SymSymExpr>(
14991499
SSE->getRHS(), Op, SSE->getLHS(), SSE->getType());
15001500
if (const RangeSet *Range = getConstraint(State, Commuted))
15011501
return *Range;
@@ -1540,15 +1540,16 @@ class SymbolicRangeInferrer
15401540

15411541
// Let's find an expression e.g. (x < y).
15421542
BinaryOperatorKind QueriedOP = OperatorRelationsTable::getOpFromIndex(i);
1543-
const SymSymExpr *SymSym = SymMgr.getSymSymExpr(LHS, QueriedOP, RHS, T);
1543+
const SymSymExpr *SymSym =
1544+
SymMgr.acquire<SymSymExpr>(LHS, QueriedOP, RHS, T);
15441545
const RangeSet *QueriedRangeSet = getConstraint(State, SymSym);
15451546

15461547
// If ranges were not previously found,
15471548
// try to find a reversed expression (y > x).
15481549
if (!QueriedRangeSet) {
15491550
const BinaryOperatorKind ROP =
15501551
BinaryOperator::reverseComparisonOp(QueriedOP);
1551-
SymSym = SymMgr.getSymSymExpr(RHS, ROP, LHS, T);
1552+
SymSym = SymMgr.acquire<SymSymExpr>(RHS, ROP, LHS, T);
15521553
QueriedRangeSet = getConstraint(State, SymSym);
15531554
}
15541555

clang/lib/StaticAnalyzer/Core/RangedConstraintManager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State,
6262

6363
SymbolManager &SymMgr = getSymbolManager();
6464
QualType DiffTy = SymMgr.getContext().getPointerDiffType();
65-
SymbolRef Subtraction =
66-
SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
65+
SymbolRef Subtraction = SymMgr.acquire<SymSymExpr>(
66+
SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
6767

6868
const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
6969
Op = BinaryOperator::reverseComparisonOp(Op);
@@ -76,8 +76,8 @@ ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State,
7676
SymbolManager &SymMgr = getSymbolManager();
7777

7878
QualType ExprType = SSE->getType();
79-
SymbolRef CanonicalEquality =
80-
SymMgr.getSymSymExpr(SSE->getLHS(), BO_EQ, SSE->getRHS(), ExprType);
79+
SymbolRef CanonicalEquality = SymMgr.acquire<SymSymExpr>(
80+
SSE->getLHS(), BO_EQ, SSE->getRHS(), ExprType);
8181

8282
bool WasEqual = SSE->getOpcode() == BO_EQ;
8383
bool IsExpectedEqual = WasEqual == Assumption;

clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,30 @@ nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *lhs,
7979
APSIntPtr rhs, QualType type) {
8080
assert(lhs);
8181
assert(!Loc::isLocType(type));
82-
return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
82+
return nonloc::SymbolVal(SymMgr.acquire<SymIntExpr>(lhs, op, rhs, type));
8383
}
8484

8585
nonloc::SymbolVal SValBuilder::makeNonLoc(APSIntPtr lhs,
8686
BinaryOperator::Opcode op,
8787
const SymExpr *rhs, QualType type) {
8888
assert(rhs);
8989
assert(!Loc::isLocType(type));
90-
return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
90+
return nonloc::SymbolVal(SymMgr.acquire<IntSymExpr>(lhs, op, rhs, type));
9191
}
9292

9393
nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *lhs,
9494
BinaryOperator::Opcode op,
9595
const SymExpr *rhs, QualType type) {
9696
assert(lhs && rhs);
9797
assert(!Loc::isLocType(type));
98-
return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
98+
return nonloc::SymbolVal(SymMgr.acquire<SymSymExpr>(lhs, op, rhs, type));
9999
}
100100

101101
NonLoc SValBuilder::makeNonLoc(const SymExpr *operand, UnaryOperator::Opcode op,
102102
QualType type) {
103103
assert(operand);
104104
assert(!Loc::isLocType(type));
105-
return nonloc::SymbolVal(SymMgr.getUnarySymExpr(operand, op, type));
105+
return nonloc::SymbolVal(SymMgr.acquire<UnarySymExpr>(operand, op, type));
106106
}
107107

108108
nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *operand,
@@ -111,7 +111,7 @@ nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *operand,
111111
assert(!Loc::isLocType(toTy));
112112
if (fromTy == toTy)
113113
return nonloc::SymbolVal(operand);
114-
return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
114+
return nonloc::SymbolVal(SymMgr.acquire<SymbolCast>(operand, fromTy, toTy));
115115
}
116116

117117
SVal SValBuilder::convertToArrayIndex(SVal val) {
@@ -143,7 +143,7 @@ SValBuilder::getRegionValueSymbolVal(const TypedValueRegion *region) {
143143
if (!SymbolManager::canSymbolicate(T))
144144
return UnknownVal();
145145

146-
SymbolRef sym = SymMgr.getRegionValueSymbol(region);
146+
SymbolRef sym = SymMgr.acquire<SymbolRegionValue>(region);
147147

148148
if (Loc::isLocType(T))
149149
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
@@ -244,8 +244,8 @@ DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
244244
unsigned count) {
245245
assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
246246

247-
SymbolRef sym =
248-
SymMgr.getMetadataSymbol(region, expr, type, LCtx, count, symbolTag);
247+
SymbolRef sym = SymMgr.acquire<SymbolMetadata>(region, expr, type, LCtx,
248+
count, symbolTag);
249249

250250
if (Loc::isLocType(type))
251251
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
@@ -264,7 +264,7 @@ SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
264264
if (!SymbolManager::canSymbolicate(T))
265265
return UnknownVal();
266266

267-
SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
267+
SymbolRef sym = SymMgr.acquire<SymbolDerived>(parentSymbol, region);
268268

269269
if (Loc::isLocType(T))
270270
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
@@ -724,7 +724,7 @@ class EvalCastVisitor : public SValVisitor<EvalCastVisitor, SVal> {
724724
// because there are no generic region address metadata
725725
// symbols to use, only content metadata.
726726
return nonloc::SymbolVal(
727-
VB.getSymbolManager().getExtentSymbol(FTR));
727+
VB.getSymbolManager().acquire<SymbolExtent>(FTR));
728728

729729
if (const SymbolicRegion *SymR = R->getSymbolicBase()) {
730730
SymbolRef Sym = SymR->getSymbol();

clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,16 @@ static NonLoc doRearrangeUnchecked(ProgramStateRef State,
328328
// FIXME: Maybe it'd be better to have consistency in
329329
// "$x - $y" vs. "$y - $x" because those are solver's keys.
330330
if (LInt > RInt) {
331-
ResultSym = SymMgr.getSymSymExpr(RSym, BO_Sub, LSym, SymTy);
331+
ResultSym = SymMgr.acquire<SymSymExpr>(RSym, BO_Sub, LSym, SymTy);
332332
ResultOp = BinaryOperator::reverseComparisonOp(Op);
333333
ResultInt = LInt - RInt; // Opposite order!
334334
} else {
335-
ResultSym = SymMgr.getSymSymExpr(LSym, BO_Sub, RSym, SymTy);
335+
ResultSym = SymMgr.acquire<SymSymExpr>(LSym, BO_Sub, RSym, SymTy);
336336
ResultOp = Op;
337337
ResultInt = RInt - LInt; // Opposite order!
338338
}
339339
} else {
340-
ResultSym = SymMgr.getSymSymExpr(LSym, Op, RSym, SymTy);
340+
ResultSym = SymMgr.acquire<SymSymExpr>(LSym, Op, RSym, SymTy);
341341
ResultInt = (Op == BO_Add) ? (LInt + RInt) : (LInt - RInt);
342342
ResultOp = BO_Add;
343343
// Bring back the cosmetic difference.
@@ -350,8 +350,8 @@ static NonLoc doRearrangeUnchecked(ProgramStateRef State,
350350
}
351351
}
352352
APSIntPtr PersistentResultInt = BV.getValue(ResultInt);
353-
return nonloc::SymbolVal(
354-
SymMgr.getSymIntExpr(ResultSym, ResultOp, PersistentResultInt, ResultTy));
353+
return nonloc::SymbolVal(SymMgr.acquire<SymIntExpr>(
354+
ResultSym, ResultOp, PersistentResultInt, ResultTy));
355355
}
356356

357357
// Rearrange if symbol type matches the result type and if the operator is a

0 commit comments

Comments
 (0)