Skip to content

Commit 0e9e1ce

Browse files
[analyzer][NFC] Make SymExpr::classof methods constexpr (#145526)
This should enable more powerful type metaprograms. Split from #144327
1 parent 8b0d112 commit 0e9e1ce

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ class SymbolData : public SymExpr {
152152
};
153153

154154
// Implement isa<T> support.
155-
static inline bool classof(const SymExpr *SE) {
156-
Kind k = SE->getKind();
157-
return k >= BEGIN_SYMBOLS && k <= END_SYMBOLS;
155+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
156+
static constexpr bool classof(Kind K) {
157+
return K >= BEGIN_SYMBOLS && K <= END_SYMBOLS;
158158
}
159159
};
160160

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

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SymbolRegionValue : public SymbolData {
4646

4747
friend class SymExprAllocator;
4848
SymbolRegionValue(SymbolID sym, const TypedValueRegion *r)
49-
: SymbolData(SymbolRegionValueKind, sym), R(r) {
49+
: SymbolData(ClassKind, sym), R(r) {
5050
assert(r);
5151
assert(isValidTypeForSymbol(r->getValueType()));
5252
}
@@ -56,7 +56,7 @@ class SymbolRegionValue : public SymbolData {
5656
const TypedValueRegion *getRegion() const { return R; }
5757

5858
static void Profile(llvm::FoldingSetNodeID& profile, const TypedValueRegion* R) {
59-
profile.AddInteger((unsigned) SymbolRegionValueKind);
59+
profile.AddInteger((unsigned)ClassKind);
6060
profile.AddPointer(R);
6161
}
6262

@@ -72,9 +72,9 @@ class SymbolRegionValue : public SymbolData {
7272
QualType getType() const override;
7373

7474
// Implement isa<T> support.
75-
static bool classof(const SymExpr *SE) {
76-
return SE->getKind() == SymbolRegionValueKind;
77-
}
75+
static constexpr Kind ClassKind = SymbolRegionValueKind;
76+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
77+
static constexpr bool classof(Kind K) { return K == ClassKind; }
7878
};
7979

8080
/// A symbol representing the result of an expression in the case when we do
@@ -90,8 +90,8 @@ class SymbolConjured : public SymbolData {
9090
SymbolConjured(SymbolID sym, ConstCFGElementRef elem,
9191
const LocationContext *lctx, QualType t, unsigned count,
9292
const void *symbolTag)
93-
: SymbolData(SymbolConjuredKind, sym), Elem(elem), T(t), Count(count),
94-
LCtx(lctx), SymbolTag(symbolTag) {
93+
: SymbolData(ClassKind, sym), Elem(elem), T(t), Count(count), LCtx(lctx),
94+
SymbolTag(symbolTag) {
9595
assert(lctx);
9696
assert(isValidTypeForSymbol(t));
9797
}
@@ -115,7 +115,7 @@ class SymbolConjured : public SymbolData {
115115
static void Profile(llvm::FoldingSetNodeID &profile, ConstCFGElementRef Elem,
116116
const LocationContext *LCtx, QualType T, unsigned Count,
117117
const void *SymbolTag) {
118-
profile.AddInteger((unsigned)SymbolConjuredKind);
118+
profile.AddInteger((unsigned)ClassKind);
119119
profile.Add(Elem);
120120
profile.AddPointer(LCtx);
121121
profile.Add(T);
@@ -128,9 +128,9 @@ class SymbolConjured : public SymbolData {
128128
}
129129

130130
// Implement isa<T> support.
131-
static bool classof(const SymExpr *SE) {
132-
return SE->getKind() == SymbolConjuredKind;
133-
}
131+
static constexpr Kind ClassKind = SymbolConjuredKind;
132+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
133+
static constexpr bool classof(Kind K) { return K == ClassKind; }
134134
};
135135

136136
/// A symbol representing the value of a MemRegion whose parent region has
@@ -141,7 +141,7 @@ class SymbolDerived : public SymbolData {
141141

142142
friend class SymExprAllocator;
143143
SymbolDerived(SymbolID sym, SymbolRef parent, const TypedValueRegion *r)
144-
: SymbolData(SymbolDerivedKind, sym), parentSymbol(parent), R(r) {
144+
: SymbolData(ClassKind, sym), parentSymbol(parent), R(r) {
145145
assert(parent);
146146
assert(r);
147147
assert(isValidTypeForSymbol(r->getValueType()));
@@ -162,7 +162,7 @@ class SymbolDerived : public SymbolData {
162162

163163
static void Profile(llvm::FoldingSetNodeID& profile, SymbolRef parent,
164164
const TypedValueRegion *r) {
165-
profile.AddInteger((unsigned) SymbolDerivedKind);
165+
profile.AddInteger((unsigned)ClassKind);
166166
profile.AddPointer(r);
167167
profile.AddPointer(parent);
168168
}
@@ -172,9 +172,9 @@ class SymbolDerived : public SymbolData {
172172
}
173173

174174
// Implement isa<T> support.
175-
static bool classof(const SymExpr *SE) {
176-
return SE->getKind() == SymbolDerivedKind;
177-
}
175+
static constexpr Kind ClassKind = SymbolDerivedKind;
176+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
177+
static constexpr bool classof(Kind K) { return K == ClassKind; }
178178
};
179179

180180
/// SymbolExtent - Represents the extent (size in bytes) of a bounded region.
@@ -185,7 +185,7 @@ class SymbolExtent : public SymbolData {
185185

186186
friend class SymExprAllocator;
187187
SymbolExtent(SymbolID sym, const SubRegion *r)
188-
: SymbolData(SymbolExtentKind, sym), R(r) {
188+
: SymbolData(ClassKind, sym), R(r) {
189189
assert(r);
190190
}
191191

@@ -200,7 +200,7 @@ class SymbolExtent : public SymbolData {
200200
void dumpToStream(raw_ostream &os) const override;
201201

202202
static void Profile(llvm::FoldingSetNodeID& profile, const SubRegion *R) {
203-
profile.AddInteger((unsigned) SymbolExtentKind);
203+
profile.AddInteger((unsigned)ClassKind);
204204
profile.AddPointer(R);
205205
}
206206

@@ -209,9 +209,9 @@ class SymbolExtent : public SymbolData {
209209
}
210210

211211
// Implement isa<T> support.
212-
static bool classof(const SymExpr *SE) {
213-
return SE->getKind() == SymbolExtentKind;
214-
}
212+
static constexpr Kind ClassKind = SymbolExtentKind;
213+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
214+
static constexpr bool classof(Kind K) { return K == ClassKind; }
215215
};
216216

217217
/// SymbolMetadata - Represents path-dependent metadata about a specific region.
@@ -229,16 +229,16 @@ class SymbolMetadata : public SymbolData {
229229
const void *Tag;
230230

231231
friend class SymExprAllocator;
232-
SymbolMetadata(SymbolID sym, const MemRegion* r, const Stmt *s, QualType t,
232+
SymbolMetadata(SymbolID sym, const MemRegion *r, const Stmt *s, QualType t,
233233
const LocationContext *LCtx, unsigned count, const void *tag)
234-
: SymbolData(SymbolMetadataKind, sym), R(r), S(s), T(t), LCtx(LCtx),
235-
Count(count), Tag(tag) {
236-
assert(r);
237-
assert(s);
238-
assert(isValidTypeForSymbol(t));
239-
assert(LCtx);
240-
assert(tag);
241-
}
234+
: SymbolData(ClassKind, sym), R(r), S(s), T(t), LCtx(LCtx), Count(count),
235+
Tag(tag) {
236+
assert(r);
237+
assert(s);
238+
assert(isValidTypeForSymbol(t));
239+
assert(LCtx);
240+
assert(tag);
241+
}
242242

243243
public:
244244
LLVM_ATTRIBUTE_RETURNS_NONNULL
@@ -264,7 +264,7 @@ class SymbolMetadata : public SymbolData {
264264
static void Profile(llvm::FoldingSetNodeID &profile, const MemRegion *R,
265265
const Stmt *S, QualType T, const LocationContext *LCtx,
266266
unsigned Count, const void *Tag) {
267-
profile.AddInteger((unsigned)SymbolMetadataKind);
267+
profile.AddInteger((unsigned)ClassKind);
268268
profile.AddPointer(R);
269269
profile.AddPointer(S);
270270
profile.Add(T);
@@ -278,9 +278,9 @@ class SymbolMetadata : public SymbolData {
278278
}
279279

280280
// Implement isa<T> support.
281-
static bool classof(const SymExpr *SE) {
282-
return SE->getKind() == SymbolMetadataKind;
283-
}
281+
static constexpr Kind ClassKind = SymbolMetadataKind;
282+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
283+
static constexpr bool classof(Kind K) { return K == ClassKind; }
284284
};
285285

286286
/// Represents a cast expression.
@@ -295,7 +295,7 @@ class SymbolCast : public SymExpr {
295295

296296
friend class SymExprAllocator;
297297
SymbolCast(SymbolID Sym, const SymExpr *In, QualType From, QualType To)
298-
: SymExpr(SymbolCastKind, Sym), Operand(In), FromTy(From), ToTy(To) {
298+
: SymExpr(ClassKind, Sym), Operand(In), FromTy(From), ToTy(To) {
299299
assert(In);
300300
assert(isValidTypeForSymbol(From));
301301
// FIXME: GenericTaintChecker creates symbols of void type.
@@ -318,7 +318,7 @@ class SymbolCast : public SymExpr {
318318

319319
static void Profile(llvm::FoldingSetNodeID& ID,
320320
const SymExpr *In, QualType From, QualType To) {
321-
ID.AddInteger((unsigned) SymbolCastKind);
321+
ID.AddInteger((unsigned)ClassKind);
322322
ID.AddPointer(In);
323323
ID.Add(From);
324324
ID.Add(To);
@@ -329,9 +329,9 @@ class SymbolCast : public SymExpr {
329329
}
330330

331331
// Implement isa<T> support.
332-
static bool classof(const SymExpr *SE) {
333-
return SE->getKind() == SymbolCastKind;
334-
}
332+
static constexpr Kind ClassKind = SymbolCastKind;
333+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
334+
static constexpr bool classof(Kind K) { return K == ClassKind; }
335335
};
336336

337337
/// Represents a symbolic expression involving a unary operator.
@@ -343,7 +343,7 @@ class UnarySymExpr : public SymExpr {
343343
friend class SymExprAllocator;
344344
UnarySymExpr(SymbolID Sym, const SymExpr *In, UnaryOperator::Opcode Op,
345345
QualType T)
346-
: SymExpr(UnarySymExprKind, Sym), Operand(In), Op(Op), T(T) {
346+
: SymExpr(ClassKind, Sym), Operand(In), Op(Op), T(T) {
347347
// Note, some unary operators are modeled as a binary operator. E.g. ++x is
348348
// modeled as x + 1.
349349
assert((Op == UO_Minus || Op == UO_Not) && "non-supported unary expression");
@@ -369,7 +369,7 @@ class UnarySymExpr : public SymExpr {
369369

370370
static void Profile(llvm::FoldingSetNodeID &ID, const SymExpr *In,
371371
UnaryOperator::Opcode Op, QualType T) {
372-
ID.AddInteger((unsigned)UnarySymExprKind);
372+
ID.AddInteger((unsigned)ClassKind);
373373
ID.AddPointer(In);
374374
ID.AddInteger(Op);
375375
ID.Add(T);
@@ -380,9 +380,9 @@ class UnarySymExpr : public SymExpr {
380380
}
381381

382382
// Implement isa<T> support.
383-
static bool classof(const SymExpr *SE) {
384-
return SE->getKind() == UnarySymExprKind;
385-
}
383+
static constexpr Kind ClassKind = UnarySymExprKind;
384+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
385+
static constexpr bool classof(Kind K) { return K == ClassKind; }
386386
};
387387

388388
/// Represents a symbolic expression involving a binary operator
@@ -408,9 +408,9 @@ class BinarySymExpr : public SymExpr {
408408
BinaryOperator::Opcode getOpcode() const { return Op; }
409409

410410
// Implement isa<T> support.
411-
static bool classof(const SymExpr *SE) {
412-
Kind k = SE->getKind();
413-
return k >= BEGIN_BINARYSYMEXPRS && k <= END_BINARYSYMEXPRS;
411+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
412+
static constexpr bool classof(Kind K) {
413+
return K >= BEGIN_BINARYSYMEXPRS && K <= END_BINARYSYMEXPRS;
414414
}
415415

416416
protected:
@@ -430,7 +430,7 @@ class BinarySymExpr : public SymExpr {
430430
};
431431

432432
/// Template implementation for all binary symbolic expressions
433-
template <class LHSTYPE, class RHSTYPE, SymExpr::Kind ClassKind>
433+
template <class LHSTYPE, class RHSTYPE, SymExpr::Kind ClassK>
434434
class BinarySymExprImpl : public BinarySymExpr {
435435
LHSTYPE LHS;
436436
RHSTYPE RHS;
@@ -474,7 +474,9 @@ class BinarySymExprImpl : public BinarySymExpr {
474474
}
475475

476476
// Implement isa<T> support.
477-
static bool classof(const SymExpr *SE) { return SE->getKind() == ClassKind; }
477+
static constexpr Kind ClassKind = ClassK;
478+
static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
479+
static constexpr bool classof(Kind K) { return K == ClassKind; }
478480
};
479481

480482
/// Represents a symbolic expression like 'x' + 3.

0 commit comments

Comments
 (0)