Skip to content

Commit

Permalink
[analyzer] ObjCGenerics: Don't warn on cast conversions involving exp…
Browse files Browse the repository at this point in the history
…licit cast

The ObjCGenerics checker warns on a cast when there is no subtyping relationship
between the tracked type of the value and the destination type of the cast. It
does this even if the cast was explicitly written. This means the user can't
write an explicit cast to silence the diagnostic.

This commit treats explicit casts involving generic types as an indication from
the programmer that the Objective-C type system is not rich enough to express
the needed invariant. On explicit casts, the checker now removes any existing
information inferred about the type arguments. Further, it no longer assumes
the casted-to specialized type because the invariant the programmer specifies
in the cast may only hold at a particular program point and not later ones. This
prevents a suppressing cast from requiring a cascade of casts down the
line.

rdar://problem/33603303

Differential Revision: https://reviews.llvm.org/D39711

llvm-svn: 318054
  • Loading branch information
devincoughlin committed Nov 13, 2017
1 parent 7822fd8 commit 5df6b94
Show file tree
Hide file tree
Showing 2 changed files with 580 additions and 613 deletions.
32 changes: 16 additions & 16 deletions clang/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
Expand Up @@ -546,8 +546,6 @@ void DynamicTypePropagation::checkPostStmt(const CastExpr *CE,
OrigObjectPtrType = OrigObjectPtrType->stripObjCKindOfTypeAndQuals(ASTCtxt);
DestObjectPtrType = DestObjectPtrType->stripObjCKindOfTypeAndQuals(ASTCtxt);

// TODO: erase tracked information when there is a cast to unrelated type
// and everything is unspecialized statically.
if (OrigObjectPtrType->isUnspecialized() &&
DestObjectPtrType->isUnspecialized())
return;
Expand All @@ -556,29 +554,31 @@ void DynamicTypePropagation::checkPostStmt(const CastExpr *CE,
if (!Sym)
return;

// Check which assignments are legal.
bool OrigToDest =
ASTCtxt.canAssignObjCInterfaces(DestObjectPtrType, OrigObjectPtrType);
bool DestToOrig =
ASTCtxt.canAssignObjCInterfaces(OrigObjectPtrType, DestObjectPtrType);
const ObjCObjectPointerType *const *TrackedType =
State->get<MostSpecializedTypeArgsMap>(Sym);

// Downcasts and upcasts handled in an uniform way regardless of being
// explicit. Explicit casts however can happen between mismatched types.
if (isa<ExplicitCastExpr>(CE) && !OrigToDest && !DestToOrig) {
// Mismatched types. If the DestType specialized, store it. Forget the
// tracked type otherwise.
if (DestObjectPtrType->isSpecialized()) {
State = State->set<MostSpecializedTypeArgsMap>(Sym, DestObjectPtrType);
C.addTransition(State, AfterTypeProp);
} else if (TrackedType) {
if (isa<ExplicitCastExpr>(CE)) {
// Treat explicit casts as an indication from the programmer that the
// Objective-C type system is not rich enough to express the needed
// invariant. In such cases, forget any existing information inferred
// about the type arguments. We don't assume the casted-to specialized
// type here because the invariant the programmer specifies in the cast
// may only hold at this particular program point and not later ones.
// We don't want a suppressing cast to require a cascade of casts down the
// line.
if (TrackedType) {
State = State->remove<MostSpecializedTypeArgsMap>(Sym);
C.addTransition(State, AfterTypeProp);
}
return;
}

// Check which assignments are legal.
bool OrigToDest =
ASTCtxt.canAssignObjCInterfaces(DestObjectPtrType, OrigObjectPtrType);
bool DestToOrig =
ASTCtxt.canAssignObjCInterfaces(OrigObjectPtrType, DestObjectPtrType);

// The tracked type should be the sub or super class of the static destination
// type. When an (implicit) upcast or a downcast happens according to static
// types, and there is no subtyping relationship between the tracked and the
Expand Down

0 comments on commit 5df6b94

Please sign in to comment.