1 //== DynamicTypeChecker.cpp ------------------------------------ -*- C++ -*--=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This checker looks for cases where the dynamic type of an object is unrelated
11 // to its static type. The type information utilized by this check is collected
12 // by the DynamicTypePropagation checker. This check does not report any type
13 // error for ObjC Generic types, in order to avoid duplicate erros from the
14 // ObjC Generics checker. This checker is not supposed to modify the program
15 // state, it is just the observer of the type information provided by other
18 //===----------------------------------------------------------------------===//
20 #include "ClangSACheckers.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
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
42 : public BugReporterVisitorImpl<DynamicTypeBugVisitor> {
44 DynamicTypeBugVisitor(const MemRegion *Reg) : Reg(Reg) {}
46 void Profile(llvm::FoldingSetNodeID &ID) const override {
52 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
53 const ExplodedNode *PrevN,
54 BugReporterContext &BRC,
55 BugReport &BR) override;
58 // The tracked region.
62 void reportTypeError(QualType DynamicType, QualType StaticType,
63 const MemRegion *Reg, const Stmt *ReportedNode,
64 CheckerContext &C) const;
67 void checkPostStmt(const ImplicitCastExpr *CE, CheckerContext &C) const;
71 void DynamicTypeChecker::reportTypeError(QualType DynamicType,
74 const Stmt *ReportedNode,
75 CheckerContext &C) const {
78 llvm::raw_svector_ostream OS(Buf);
79 OS << "Object has a dynamic type '";
80 QualType::print(DynamicType.getTypePtr(), Qualifiers(), OS, C.getLangOpts(),
82 OS << "' which is incompatible with static type '";
83 QualType::print(StaticType.getTypePtr(), Qualifiers(), OS, C.getLangOpts(),
86 std::unique_ptr<BugReport> R(
87 new BugReport(*BT, OS.str(), C.generateNonFatalErrorNode()));
88 R->markInteresting(Reg);
89 R->addVisitor(llvm::make_unique<DynamicTypeBugVisitor>(Reg));
90 R->addRange(ReportedNode->getSourceRange());
91 C.emitReport(std::move(R));
94 PathDiagnosticPiece *DynamicTypeChecker::DynamicTypeBugVisitor::VisitNode(
95 const ExplodedNode *N, const ExplodedNode *PrevN, BugReporterContext &BRC,
97 ProgramStateRef State = N->getState();
98 ProgramStateRef StatePrev = PrevN->getState();
100 DynamicTypeInfo TrackedType = getDynamicTypeInfo(State, Reg);
101 DynamicTypeInfo TrackedTypePrev = getDynamicTypeInfo(StatePrev, Reg);
102 if (!TrackedType.isValid())
105 if (TrackedTypePrev.isValid() &&
106 TrackedTypePrev.getType() == TrackedType.getType())
109 // Retrieve the associated statement.
110 const Stmt *S = nullptr;
111 ProgramPoint ProgLoc = N->getLocation();
112 if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
119 const LangOptions &LangOpts = BRC.getASTContext().getLangOpts();
121 SmallString<256> Buf;
122 llvm::raw_svector_ostream OS(Buf);
124 QualType::print(TrackedType.getType().getTypePtr(), Qualifiers(), OS,
125 LangOpts, llvm::Twine());
126 OS << "' is inferred from ";
128 if (const auto *ExplicitCast = dyn_cast<ExplicitCastExpr>(S)) {
129 OS << "explicit cast (from '";
130 QualType::print(ExplicitCast->getSubExpr()->getType().getTypePtr(),
131 Qualifiers(), OS, LangOpts, llvm::Twine());
133 QualType::print(ExplicitCast->getType().getTypePtr(), Qualifiers(), OS,
134 LangOpts, llvm::Twine());
136 } else if (const auto *ImplicitCast = dyn_cast<ImplicitCastExpr>(S)) {
137 OS << "implicit cast (from '";
138 QualType::print(ImplicitCast->getSubExpr()->getType().getTypePtr(),
139 Qualifiers(), OS, LangOpts, llvm::Twine());
141 QualType::print(ImplicitCast->getType().getTypePtr(), Qualifiers(), OS,
142 LangOpts, llvm::Twine());
145 OS << "this context";
148 // Generate the extra diagnostic.
149 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
150 N->getLocationContext());
151 return new PathDiagnosticEventPiece(Pos, OS.str(), true, nullptr);
154 static bool hasDefinition(const ObjCObjectPointerType *ObjPtr) {
155 const ObjCInterfaceDecl *Decl = ObjPtr->getInterfaceDecl();
159 return Decl->getDefinition();
162 // TODO: consider checking explicit casts?
163 void DynamicTypeChecker::checkPostStmt(const ImplicitCastExpr *CE,
164 CheckerContext &C) const {
165 // TODO: C++ support.
166 if (CE->getCastKind() != CK_BitCast)
169 const MemRegion *Region = C.getSVal(CE).getAsRegion();
173 ProgramStateRef State = C.getState();
174 DynamicTypeInfo DynTypeInfo = getDynamicTypeInfo(State, Region);
176 if (!DynTypeInfo.isValid())
179 QualType DynType = DynTypeInfo.getType();
180 QualType StaticType = CE->getType();
182 const auto *DynObjCType = DynType->getAs<ObjCObjectPointerType>();
183 const auto *StaticObjCType = StaticType->getAs<ObjCObjectPointerType>();
185 if (!DynObjCType || !StaticObjCType)
188 if (!hasDefinition(DynObjCType) || !hasDefinition(StaticObjCType))
191 ASTContext &ASTCtxt = C.getASTContext();
193 // Strip kindeofness to correctly detect subtyping relationships.
194 DynObjCType = DynObjCType->stripObjCKindOfTypeAndQuals(ASTCtxt);
195 StaticObjCType = StaticObjCType->stripObjCKindOfTypeAndQuals(ASTCtxt);
197 // Specialized objects are handled by the generics checker.
198 if (StaticObjCType->isSpecialized())
201 if (ASTCtxt.canAssignObjCInterfaces(StaticObjCType, DynObjCType))
204 if (DynTypeInfo.canBeASubClass() &&
205 ASTCtxt.canAssignObjCInterfaces(DynObjCType, StaticObjCType))
208 reportTypeError(DynType, StaticType, Region, CE, C);
211 void ento::registerDynamicTypeChecker(CheckerManager &mgr) {
212 mgr.registerChecker<DynamicTypeChecker>();