29 using namespace clang;
33 class DynamicTypeChecker :
public Checker<check::PostStmt<ImplicitCastExpr>> {
34 mutable std::unique_ptr<BugType> BT;
35 void initBugType()
const {
38 new BugType(
this,
"Dynamic and static type mismatch",
"Type Error"));
41 class DynamicTypeBugVisitor
44 DynamicTypeBugVisitor(
const MemRegion *Reg) : Reg(Reg) {}
46 void Profile(llvm::FoldingSetNodeID &
ID)
const override {
71 void DynamicTypeChecker::reportTypeError(
QualType DynamicType,
74 const Stmt *ReportedNode,
78 llvm::raw_svector_ostream OS(Buf);
79 OS <<
"Object has a dynamic type '";
82 OS <<
"' which is incompatible with static type '";
86 std::unique_ptr<BugReport> R(
88 R->markInteresting(Reg);
89 R->addVisitor(llvm::make_unique<DynamicTypeBugVisitor>(Reg));
90 R->addRange(ReportedNode->getSourceRange());
105 if (TrackedTypePrev.
isValid() &&
110 const Stmt *
S =
nullptr;
122 llvm::raw_svector_ostream OS(Buf);
125 LangOpts, llvm::Twine());
126 OS <<
"' is inferred from ";
128 if (
const auto *ExplicitCast = dyn_cast<ExplicitCastExpr>(S)) {
129 OS <<
"explicit cast (from '";
134 LangOpts, llvm::Twine());
136 }
else if (
const auto *ImplicitCast = dyn_cast<ImplicitCastExpr>(S)) {
137 OS <<
"implicit cast (from '";
142 LangOpts, llvm::Twine());
145 OS <<
"this context";
185 if (!DynObjCType || !StaticObjCType)
194 DynObjCType = DynObjCType->stripObjCKindOfTypeAndQuals(ASTCtxt);
208 reportTypeError(DynType, StaticType, Region, CE, C);
CastKind getCastKind() const
A (possibly-)qualified type.
MemRegion - The root abstract class for all memory regions.
bool canBeASubClass() const
Returns false if the type information is precise (the type T is the only type in the lattice)...
ASTContext & getASTContext()
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
bool isSpecialized() const
Whether this type is specialized, meaning that it has type arguments.
The collection of all-type qualifiers we support.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
This class provides a convenience implementation for clone() using the Curiously-Recurring Template P...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const LangOptions & getLangOpts() const
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
Represents an ObjC class declaration.
const LocationContext * getLocationContext() const
const ProgramStateRef & getState() const
const ProgramStateRef & getState() const
Stores the currently inferred strictest bound on the runtime type of a region in a given state along ...
const ObjCObjectPointerType * stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const
Strip off the Objective-C "kindof" type and (with it) any protocol qualifiers.
QualType getType() const
Returns the currently inferred upper bound on the runtime type.
static bool hasDefinition(const ObjCObjectPointerType *ObjPtr)
ExplodedNode * generateNonFatalErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx)
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
ASTContext & getASTContext()
bool isValid() const
Return false if no dynamic type info is available.
DynamicTypeInfo getDynamicTypeInfo(ProgramStateRef State, const MemRegion *Reg)
Get dynamic type information for a region.
ObjCInterfaceDecl * getDefinition()
Retrieve the definition of this class, or NULL if this class has been forward-declared (with @class) ...
const MemRegion * getAsRegion() const
Optional< T > getAs() const
Convert to the specified ProgramPoint type, returning None if this ProgramPoint is not of the desired...
Represents a pointer to an Objective C object.
const LangOptions & getLangOpts() const
const T * getAs() const
Member-template getAs<specific type>'.
ObjCInterfaceDecl * getInterfaceDecl() const
If this pointer points to an Objective @interface type, gets the declaration for that interface...
bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, const ObjCObjectPointerType *RHSOPT)
canAssignObjCInterfaces - Return true if the two interface types are compatible for assignment from R...
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
This class provides an interface through which checkers can create individual bug reports...
SourceManager & getSourceManager()
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.