Skip to content

[analyzer] Conversion to CheckerFamily: DynamicTypePropagation #144735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,12 @@ def TestAfterDivZeroChecker : Checker<"TestAfterDivZero">,
"Either the comparison is useless or there is division by zero.">,
Documentation<HasDocumentation>;

def DynamicTypeChecker : Checker<"DynamicTypeChecker">,
HelpText<"Check for cases where the dynamic and the static type of an object "
"are unrelated.">,
Documentation<HasDocumentation>;
def DynamicTypeChecker
: Checker<"DynamicTypeChecker">,
HelpText<"Check for cases where the dynamic and the static type of an "
"object are unrelated.">,
Dependencies<[DynamicTypePropagation]>,
Documentation<HasDocumentation>;

def StackAddrAsyncEscapeChecker : Checker<"StackAddressAsyncEscape">,
HelpText<"Check that addresses to stack memory do not escape the function">,
Expand Down
55 changes: 26 additions & 29 deletions clang/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ REGISTER_MAP_WITH_PROGRAMSTATE(MostSpecializedTypeArgsMap, SymbolRef,
const ObjCObjectPointerType *)

namespace {
class DynamicTypePropagation:
public Checker< check::PreCall,
check::PostCall,
check::DeadSymbols,
check::PostStmt<CastExpr>,
check::PostStmt<CXXNewExpr>,
check::PreObjCMessage,
check::PostObjCMessage > {
class DynamicTypePropagation
: public CheckerFamily<check::PreCall, check::PostCall, check::DeadSymbols,
check::PostStmt<CastExpr>,
check::PostStmt<CXXNewExpr>, check::PreObjCMessage,
check::PostObjCMessage> {
public:
// This checker family implements only one frontend, but -- unlike a simple
// Checker -- its backend can be enabled (by the checker DynamicTypeChecker
// which depends on it) without enabling the frontend.
CheckerFrontendWithBugType ObjCGenericsChecker{
"Generics", categories::CoreFoundationObjectiveC};

private:
/// Return a better dynamic type if one can be derived from the cast.
const ObjCObjectPointerType *getBetterObjCType(const Expr *CastE,
CheckerContext &C) const;
Expand All @@ -66,13 +70,6 @@ class DynamicTypePropagation:
ProgramStateRef &State,
CheckerContext &C) const;

mutable std::unique_ptr<BugType> ObjCGenericsBugType;
void initBugType() const {
if (!ObjCGenericsBugType)
ObjCGenericsBugType.reset(new BugType(
GenericCheckName, "Generics", categories::CoreFoundationObjectiveC));
}

class GenericsBugVisitor : public BugReporterVisitor {
public:
GenericsBugVisitor(SymbolRef S) : Sym(S) {}
Expand Down Expand Up @@ -106,9 +103,8 @@ class DynamicTypePropagation:
void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;

/// This value is set to true, when the Generics checker is turned on.
bool CheckGenerics = false;
CheckerNameRef GenericCheckName;
/// Identifies this checker family for debugging purposes.
StringRef getDebugTag() const override { return "DynamicTypePropagation"; }
};

bool isObjCClassType(QualType Type) {
Expand Down Expand Up @@ -1026,18 +1022,17 @@ void DynamicTypePropagation::reportGenericsBug(
const ObjCObjectPointerType *From, const ObjCObjectPointerType *To,
ExplodedNode *N, SymbolRef Sym, CheckerContext &C,
const Stmt *ReportedNode) const {
if (!CheckGenerics)
if (!ObjCGenericsChecker.isEnabled())
return;

initBugType();
SmallString<192> Buf;
llvm::raw_svector_ostream OS(Buf);
OS << "Conversion from value of type '";
QualType::print(From, Qualifiers(), OS, C.getLangOpts(), llvm::Twine());
OS << "' to incompatible type '";
QualType::print(To, Qualifiers(), OS, C.getLangOpts(), llvm::Twine());
OS << "'";
auto R = std::make_unique<PathSensitiveBugReport>(*ObjCGenericsBugType,
auto R = std::make_unique<PathSensitiveBugReport>(ObjCGenericsChecker,
OS.str(), N);
R->markInteresting(Sym);
R->addVisitor(std::make_unique<GenericsBugVisitor>(Sym));
Expand Down Expand Up @@ -1102,20 +1097,22 @@ PathDiagnosticPieceRef DynamicTypePropagation::GenericsBugVisitor::VisitNode(
}

/// Register checkers.
void ento::registerObjCGenericsChecker(CheckerManager &mgr) {
DynamicTypePropagation *checker = mgr.getChecker<DynamicTypePropagation>();
checker->CheckGenerics = true;
checker->GenericCheckName = mgr.getCurrentCheckerName();
void ento::registerObjCGenericsChecker(CheckerManager &Mgr) {
Mgr.getChecker<DynamicTypePropagation>()->ObjCGenericsChecker.enable(Mgr);
}

bool ento::shouldRegisterObjCGenericsChecker(const CheckerManager &mgr) {
bool ento::shouldRegisterObjCGenericsChecker(const CheckerManager &) {
return true;
}

void ento::registerDynamicTypePropagation(CheckerManager &mgr) {
mgr.registerChecker<DynamicTypePropagation>();
void ento::registerDynamicTypePropagation(CheckerManager &Mgr) {
// The checker 'core.DynamicTypeChecker' relies on the modeling implemented
// in the class 'DynamicTypePropagation', so this "modeling checker" can
// register the 'DynamicTypePropagation' backend for its callbacks without
// enabling its frontend.
Mgr.getChecker<DynamicTypePropagation>();
}

bool ento::shouldRegisterDynamicTypePropagation(const CheckerManager &mgr) {
bool ento::shouldRegisterDynamicTypePropagation(const CheckerManager &) {
return true;
}
Loading