clang 24.0.0git
SValExplainer.h
Go to the documentation of this file.
1//== SValExplainer.h - Symbolic value explainer -----------------*- C++ -*--==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines SValExplainer, a class for pretty-printing a
10// human-readable description of a symbolic value. For example,
11// "reg_$0<x>" is turned into "initial value of variable 'x'".
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_STATICANALYZER_CHECKERS_SVALEXPLAINER_H
16#define LLVM_CLANG_STATICANALYZER_CHECKERS_SVALEXPLAINER_H
17
18#include "clang/AST/Attr.h"
19#include "clang/AST/DeclCXX.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/raw_ostream.h"
24
25namespace clang {
26
27namespace ento {
28
29class SValExplainer : public FullSValVisitor<SValExplainer, std::string> {
30private:
31 ASTContext &ACtx;
32 ProgramStateRef State;
33
34 std::string printCFGElementRef(ConstCFGElementRef Elem) {
35 std::string Str;
36 llvm::raw_string_ostream OS(Str);
37 Elem->dumpToStream(OS, /*TerminateWithNewLine=*/false);
38 return Str;
39 }
40
41 std::string printStmt(const Stmt *S) {
42 std::string Str;
43 llvm::raw_string_ostream OS(Str);
44 S->printPretty(OS, nullptr, PrintingPolicy(ACtx.getLangOpts()));
45 return Str;
46 }
47
48 bool isThisObject(const SymbolicRegion *R) {
49 if (auto S = dyn_cast<SymbolRegionValue>(R->getSymbol()))
50 if (isa<CXXThisRegion>(S->getRegion()))
51 return true;
52 return false;
53 }
54
55 bool isThisObject(const ElementRegion *R) {
56 if (const auto *Idx = R->getIndex().getAsInteger()) {
57 if (const auto *SR = R->getSuperRegion()->getAs<SymbolicRegion>()) {
58 QualType Ty = SR->getPointeeStaticType();
59 bool IsNotReinterpretCast = R->getValueType() == Ty;
60 if (Idx->isZero() && IsNotReinterpretCast)
61 return isThisObject(SR);
62 }
63 }
64 return false;
65 }
66
67public:
69 : ACtx(Ctx), State(State) {}
70
72 return "unknown value";
73 }
74
76 return "undefined value";
77 }
78
80 const MemRegion *R = V.getRegion();
81 // Avoid the weird "pointer to pointee of ...".
82 if (auto SR = dyn_cast<SymbolicRegion>(R)) {
83 // However, "pointer to 'this' object" is fine.
84 if (!isThisObject(SR))
85 return Visit(SR->getSymbol());
86 }
87 return "pointer to " + Visit(R);
88 }
89
91 const llvm::APSInt &I = V.getValue();
92 std::string Str;
93 llvm::raw_string_ostream OS(Str);
94 OS << "concrete memory address '" << I << "'";
95 return Str;
96 }
97
99 return Visit(V.getSymbol());
100 }
101
103 const llvm::APSInt &I = V.getValue();
104 std::string Str;
105 llvm::raw_string_ostream OS(Str);
106 OS << (I.isSigned() ? "signed " : "unsigned ") << I.getBitWidth()
107 << "-bit integer '" << I << "'";
108 return Str;
109 }
110
112 return "lazily frozen compound value of " + Visit(V.getRegion());
113 }
114
116 const MemRegion *R = S->getRegion();
117 // Special handling for argument values.
118 if (auto V = dyn_cast<VarRegion>(R))
119 if (auto D = dyn_cast<ParmVarDecl>(V->getDecl()))
120 return "argument '" + D->getQualifiedNameAsString() + "'";
121 return "initial value of " + Visit(R);
122 }
123
124 std::string VisitSymbolConjured(const SymbolConjured *S) {
125 std::optional<ConstCFGElementRef> Elt = S->getCFGElementRef();
126 std::string Res = "symbol of type '" + S->getType().getAsString() + "'";
127 if (Elt)
128 Res += " conjured at CFG element '" + printCFGElementRef(*Elt) + "'";
129 return Res;
130 }
131
132 std::string VisitSymbolDerived(const SymbolDerived *S) {
133 return "value derived from (" + Visit(S->getParentSymbol()) +
134 ") for " + Visit(S->getRegion());
135 }
136
137 std::string VisitSymbolExtent(const SymbolExtent *S) {
138 return "extent of " + Visit(S->getRegion());
139 }
140
141 std::string VisitSymbolMetadata(const SymbolMetadata *S) {
142 return "metadata of type '" + S->getType().getAsString() + "' tied to " +
143 Visit(S->getRegion());
144 }
145
146 std::string VisitSymIntExpr(const SymIntExpr *S) {
147 std::string Str;
148 llvm::raw_string_ostream OS(Str);
149 OS << "(" << Visit(S->getLHS()) << ") "
150 << std::string(BinaryOperator::getOpcodeStr(S->getOpcode())) << " "
151 << S->getRHS();
152 return Str;
153 }
154
155 // TODO: IntSymExpr doesn't appear in practice.
156 // Add the relevant code once it does.
157
158 std::string VisitSymSymExpr(const SymSymExpr *S) {
159 return "(" + Visit(S->getLHS()) + ") " +
160 std::string(BinaryOperator::getOpcodeStr(S->getOpcode())) +
161 " (" + Visit(S->getRHS()) + ")";
162 }
163
164 std::string VisitUnarySymExpr(const UnarySymExpr *S) {
165 return std::string(UnaryOperator::getOpcodeStr(S->getOpcode())) + " (" +
166 Visit(S->getOperand()) + ")";
167 }
168
169 // TODO: SymbolCast doesn't appear in practice.
170 // Add the relevant code once it does.
171
172 std::string VisitSymbolicRegion(const SymbolicRegion *R) {
173 // Explain 'this' object here - if it's not wrapped by an ElementRegion.
174 // TODO: Explain CXXThisRegion itself, find a way to test it.
175 if (isThisObject(R))
176 return "'this' object";
177 // Objective-C objects are not normal symbolic regions. At least,
178 // they're always on the heap.
179 if (R->getSymbol()->getType()
180 .getCanonicalType()->getAs<ObjCObjectPointerType>())
181 return "object at " + Visit(R->getSymbol());
182 // Other heap-based symbolic regions are also special.
183 if (R->hasMemorySpace<HeapSpaceRegion>(State))
184 return "heap segment that starts at " + Visit(R->getSymbol());
185 return "pointee of " + Visit(R->getSymbol());
186 }
187
188 std::string VisitAllocaRegion(const AllocaRegion *R) {
189 return "region allocated by '" + printStmt(R->getExpr()) + "'";
190 }
191
193 return "compound literal " + printStmt(R->getLiteralExpr());
194 }
195
196 std::string VisitStringRegion(const StringRegion *R) {
197 return "string literal " + R->getString();
198 }
199
200 std::string VisitElementRegion(const ElementRegion *R) {
201 std::string Str;
202 llvm::raw_string_ostream OS(Str);
203
204 // Explain 'this' object here.
205 // They are represented by a SymRegion wrapped by an ElementRegion; so
206 // match and handle it here.
207 if (isThisObject(R))
208 return "'this' object";
209
210 OS << "element of type '" << R->getElementType() << "' with index ";
211 // For concrete index: omit type of the index integer.
212 if (auto I = R->getIndex().getAs<nonloc::ConcreteInt>())
213 OS << I->getValue();
214 else
215 OS << "'" << Visit(R->getIndex()) << "'";
216 OS << " of " + Visit(R->getSuperRegion());
217 return Str;
218 }
219
221 const VarDecl *VD = R->getDecl();
222 std::string Name = VD->getQualifiedNameAsString();
223 if (isa<ParmVarDecl>(VD))
224 return "parameter '" + Name + "'";
225 else if (VD->hasAttr<BlocksAttr>())
226 return "block variable '" + Name + "'";
227 else if (VD->hasLocalStorage())
228 return "local variable '" + Name + "'";
229 else if (VD->isStaticLocal())
230 return "static local variable '" + Name + "'";
231 else if (VD->hasGlobalStorage())
232 return "global variable '" + Name + "'";
233 else
234 llvm_unreachable("A variable is either local or global");
235 }
236
237 std::string VisitObjCIvarRegion(const ObjCIvarRegion *R) {
238 return "instance variable '" + R->getDecl()->getNameAsString() + "' of " +
239 Visit(R->getSuperRegion());
240 }
241
242 std::string VisitFieldRegion(const FieldRegion *R) {
243 return "field '" + R->getDecl()->getNameAsString() + "' of " +
244 Visit(R->getSuperRegion());
245 }
246
248 return "temporary object constructed at statement '" +
249 printStmt(R->getExpr()) + "'";
250 }
251
253 return "base object '" + R->getDecl()->getQualifiedNameAsString() +
254 "' inside " + Visit(R->getSuperRegion());
255 }
256
257 std::string VisitParamVarRegion(const ParamVarRegion *R) {
258 std::string Str;
259 llvm::raw_string_ostream OS(Str);
260
261 const ParmVarDecl *PVD = R->getDecl();
262 std::string Name = PVD->getQualifiedNameAsString();
263 if (!Name.empty()) {
264 OS << "parameter '" << Name << "'";
265 return std::string(OS.str());
266 }
267
268 unsigned Index = R->getIndex() + 1;
269 OS << Index << llvm::getOrdinalSuffix(Index) << " parameter of ";
270 const Decl *Parent = R->getStackFrame()->getDecl();
271 if (const auto *FD = dyn_cast<FunctionDecl>(Parent))
272 OS << "function '" << FD->getQualifiedNameAsString() << "()'";
273 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(Parent))
274 OS << "C++ constructor '" << CD->getQualifiedNameAsString() << "()'";
275 else if (const auto *MD = dyn_cast<ObjCMethodDecl>(Parent)) {
276 if (MD->isClassMethod())
277 OS << "Objective-C method '+" << MD->getQualifiedNameAsString() << "'";
278 else
279 OS << "Objective-C method '-" << MD->getQualifiedNameAsString() << "'";
280 } else if (isa<BlockDecl>(Parent)) {
281 if (cast<BlockDecl>(Parent)->isConversionFromLambda())
282 OS << "lambda";
283 else
284 OS << "block";
285 }
286
287 return std::string(OS.str());
288 }
289
290 std::string VisitSVal(SVal V) {
291 std::string Str;
292 llvm::raw_string_ostream OS(Str);
293 OS << V;
294 return "a value unsupported by the explainer: (" +
295 std::string(OS.str()) + ")";
296 }
297
298 std::string VisitSymExpr(SymbolRef S) {
299 std::string Str;
300 llvm::raw_string_ostream OS(Str);
301 S->dumpToStream(OS);
302 return "a symbolic expression unsupported by the explainer: (" +
303 std::string(OS.str()) + ")";
304 }
305
306 std::string VisitMemRegion(const MemRegion *R) {
307 std::string Str;
308 llvm::raw_string_ostream OS(Str);
309 OS << R;
310 return "a memory region unsupported by the explainer (" +
311 std::string(OS.str()) + ")";
312 }
313};
314
315} // end namespace ento
316
317} // end namespace clang
318
319#endif
#define V(N, I)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
static StringRef getOpcodeStr(Opcode Op)
getOpcodeStr - Turn an Opcode enum value into the punctuation char it corresponds to,...
Definition Expr.cpp:2171
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool hasAttr() const
Definition DeclBase.h:586
std::string getQualifiedNameAsString() const
Definition Decl.cpp:1685
Represents a pointer to an Objective C object.
Definition TypeBase.h:8321
Represents a parameter to a function.
Definition Decl.h:1832
A (possibly-)qualified type.
Definition TypeBase.h:938
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition TypeBase.h:1348
Stmt - This represents one statement.
Definition Stmt.h:85
void printPretty(raw_ostream &OS, PrinterHelper *Helper, const PrintingPolicy &Policy, unsigned Indentation=0, StringRef NewlineSymbol="\n", const ASTContext *Context=nullptr) const
static StringRef getOpcodeStr(Opcode Op)
getOpcodeStr - Turn an Opcode enum value into the punctuation char it corresponds to,...
Definition Expr.cpp:1438
Represents a variable declaration or definition.
Definition Decl.h:934
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1249
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1216
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition Decl.h:1192
AllocaRegion - A region that represents an untyped blob of bytes created by a call to 'alloca'.
Definition MemRegion.h:513
BinaryOperator::Opcode getOpcode() const
CompoundLiteralRegion - A memory region representing a compound literal.
Definition MemRegion.h:934
ElementRegion is used to represent both array elements and casts.
Definition MemRegion.h:1238
FullSValVisitor - a convenient mixed visitor for all three: SVal, SymExpr and MemRegion subclasses.
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
ParamVarRegion - Represents a region for parameters.
Definition MemRegion.h:1073
std::string VisitSymbolRegionValue(const SymbolRegionValue *S)
std::string VisitFieldRegion(const FieldRegion *R)
std::string VisitConcreteInt(nonloc::ConcreteInt V)
std::string VisitSVal(SVal V)
std::string VisitObjCIvarRegion(const ObjCIvarRegion *R)
std::string VisitParamVarRegion(const ParamVarRegion *R)
std::string VisitSymbolDerived(const SymbolDerived *S)
std::string VisitUnarySymExpr(const UnarySymExpr *S)
std::string VisitCompoundLiteralRegion(const CompoundLiteralRegion *R)
std::string VisitSymIntExpr(const SymIntExpr *S)
std::string VisitSymExpr(SymbolRef S)
std::string VisitSymbolicRegion(const SymbolicRegion *R)
std::string VisitSymbolVal(nonloc::SymbolVal V)
std::string VisitMemRegion(const MemRegion *R)
SValExplainer(ASTContext &Ctx, ProgramStateRef State)
std::string VisitUnknownVal(UnknownVal V)
std::string VisitConcreteInt(loc::ConcreteInt V)
std::string VisitCXXTempObjectRegion(const CXXTempObjectRegion *R)
std::string VisitNonParamVarRegion(const NonParamVarRegion *R)
std::string VisitCXXBaseObjectRegion(const CXXBaseObjectRegion *R)
std::string VisitSymbolMetadata(const SymbolMetadata *S)
std::string VisitSymSymExpr(const SymSymExpr *S)
std::string VisitSymbolConjured(const SymbolConjured *S)
std::string VisitSymbolExtent(const SymbolExtent *S)
std::string VisitUndefinedVal(UndefinedVal V)
std::string VisitAllocaRegion(const AllocaRegion *R)
std::string VisitElementRegion(const ElementRegion *R)
std::string VisitMemRegionVal(loc::MemRegionVal V)
std::string VisitLazyCompoundVal(nonloc::LazyCompoundVal V)
std::string VisitStringRegion(const StringRegion *R)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
StringRegion - Region associated with a StringLiteral.
Definition MemRegion.h:863
virtual void dumpToStream(raw_ostream &os) const
Definition SymExpr.h:86
A symbol representing the result of an expression in the case when we do not know anything about what...
QualType getType() const override
std::optional< ConstCFGElementRef > getCFGElementRef() const
A symbol representing the value of a MemRegion whose parent region has symbolic value.
LLVM_ATTRIBUTE_RETURNS_NONNULL SymbolRef getParentSymbol() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
SymbolExtent - Represents the extent (size in bytes) of a bounded region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const SubRegion * getRegion() const
SymbolMetadata - Represents path-dependent metadata about a specific region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getRegion() const
QualType getType() const override
A symbol representing the value stored at a MemRegion.
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
SymbolicRegion - A special, "non-concrete" region.
Definition MemRegion.h:814
Represents a symbolic expression involving a unary operator.
UnaryOperator::Opcode getOpcode() const
const SymExpr * getOperand() const
Value representing integer constant.
Definition SVals.h:303
While nonloc::CompoundVal covers a few simple use cases, nonloc::LazyCompoundVal is a more performant...
Definition SVals.h:392
Represents symbolic expression that isn't a location.
Definition SVals.h:282
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:166
BinarySymExprImpl< const SymExpr *, const SymExpr *, SymExpr::Kind::SymSymExprKind > SymSymExpr
Represents a symbolic expression like 'x' + 'y'.
BinarySymExprImpl< const SymExpr *, APSIntPtr, SymExpr::Kind::SymIntExprKind > SymIntExpr
Represents a symbolic expression like 'x' + 3.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1271
U cast(CodeGen::Address addr)
Definition Address.h:327
Describes how types, statements, expressions, and declarations should be printed.