15#ifndef LLVM_CLANG_STATICANALYZER_CHECKERS_SVALEXPLAINER_H
16#define LLVM_CLANG_STATICANALYZER_CHECKERS_SVALEXPLAINER_H
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/raw_ostream.h"
36 llvm::raw_string_ostream
OS(Str);
37 Elem->dumpToStream(
OS,
false);
41 std::string printStmt(
const Stmt *S) {
43 llvm::raw_string_ostream
OS(Str);
49 if (
auto S = dyn_cast<SymbolRegionValue>(R->getSymbol()))
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);
69 : ACtx(Ctx), State(State) {}
72 return "unknown value";
76 return "undefined value";
82 if (
auto SR = dyn_cast<SymbolicRegion>(R)) {
84 if (!isThisObject(SR))
85 return Visit(SR->getSymbol());
87 return "pointer to " +
Visit(R);
91 const llvm::APSInt &I =
V.getValue();
93 llvm::raw_string_ostream
OS(Str);
94 OS <<
"concrete memory address '" << I <<
"'";
99 return Visit(
V.getSymbol());
103 const llvm::APSInt &I =
V.getValue();
105 llvm::raw_string_ostream
OS(Str);
106 OS << (I.isSigned() ?
"signed " :
"unsigned ") << I.getBitWidth()
107 <<
"-bit integer '" << I <<
"'";
112 return "lazily frozen compound value of " +
Visit(
V.getRegion());
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);
128 Res +=
" conjured at CFG element '" + printCFGElementRef(*Elt) +
"'";
148 llvm::raw_string_ostream
OS(Str);
176 return "'this' object";
179 if (R->getSymbol()->getType()
181 return "object at " +
Visit(R->getSymbol());
184 return "heap segment that starts at " +
Visit(R->getSymbol());
185 return "pointee of " +
Visit(R->getSymbol());
189 return "region allocated by '" + printStmt(R->getExpr()) +
"'";
193 return "compound literal " + printStmt(R->getLiteralExpr());
197 return "string literal " + R->getString();
202 llvm::raw_string_ostream
OS(Str);
208 return "'this' object";
210 OS <<
"element of type '" << R->getElementType() <<
"' with index ";
215 OS <<
"'" <<
Visit(R->getIndex()) <<
"'";
216 OS <<
" of " +
Visit(R->getSuperRegion());
221 const VarDecl *VD = R->getDecl();
224 return "parameter '" + Name +
"'";
225 else if (VD->
hasAttr<BlocksAttr>())
226 return "block variable '" + Name +
"'";
228 return "local variable '" + Name +
"'";
230 return "static local variable '" + Name +
"'";
232 return "global variable '" + Name +
"'";
234 llvm_unreachable(
"A variable is either local or global");
238 return "instance variable '" + R->getDecl()->getNameAsString() +
"' of " +
239 Visit(R->getSuperRegion());
243 return "field '" + R->getDecl()->getNameAsString() +
"' of " +
244 Visit(R->getSuperRegion());
248 return "temporary object constructed at statement '" +
249 printStmt(R->getExpr()) +
"'";
253 return "base object '" + R->getDecl()->getQualifiedNameAsString() +
254 "' inside " +
Visit(R->getSuperRegion());
259 llvm::raw_string_ostream
OS(Str);
264 OS <<
"parameter '" << Name <<
"'";
265 return std::string(
OS.str());
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() <<
"'";
279 OS <<
"Objective-C method '-" << MD->getQualifiedNameAsString() <<
"'";
287 return std::string(
OS.str());
292 llvm::raw_string_ostream
OS(Str);
294 return "a value unsupported by the explainer: (" +
295 std::string(
OS.str()) +
")";
300 llvm::raw_string_ostream
OS(Str);
302 return "a symbolic expression unsupported by the explainer: (" +
303 std::string(
OS.str()) +
")";
308 llvm::raw_string_ostream
OS(Str);
310 return "a memory region unsupported by the explainer (" +
311 std::string(
OS.str()) +
")";
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 ...
static StringRef getOpcodeStr(Opcode Op)
getOpcodeStr - Turn an Opcode enum value into the punctuation char it corresponds to,...
Decl - This represents one declaration (or definition), e.g.
std::string getQualifiedNameAsString() const
Represents a pointer to an Objective C object.
Represents a parameter to a function.
A (possibly-)qualified type.
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Stmt - This represents one statement.
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,...
Represents a variable declaration or definition.
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
AllocaRegion - A region that represents an untyped blob of bytes created by a call to 'alloca'.
BinaryOperator::Opcode getOpcode() const
CompoundLiteralRegion - A memory region representing a compound literal.
ElementRegion is used to represent both array elements and casts.
FullSValVisitor - a convenient mixed visitor for all three: SVal, SymExpr and MemRegion subclasses.
MemRegion - The root abstract class for all memory regions.
ParamVarRegion - Represents a region for parameters.
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)
std::string Visit(SVal V)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
StringRegion - Region associated with a StringLiteral.
virtual void dumpToStream(raw_ostream &os) const
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
A symbol representing the value stored at a MemRegion.
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
SymbolicRegion - A special, "non-concrete" region.
Represents a symbolic expression involving a unary operator.
UnaryOperator::Opcode getOpcode() const
const SymExpr * getOperand() const
Value representing integer constant.
While nonloc::CompoundVal covers a few simple use cases, nonloc::LazyCompoundVal is a more performant...
Represents symbolic expression that isn't a location.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
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)
CFGBlock::ConstCFGElementRef ConstCFGElementRef
U cast(CodeGen::Address addr)
Describes how types, statements, expressions, and declarations should be printed.