[analyzer] ObjCGenerics: Warn only on mismatch for invariant type parameters
authorDevin Coughlin <dcoughlin@apple.com>
Mon, 5 Dec 2016 16:28:47 +0000 (16:28 +0000)
committerDevin Coughlin <dcoughlin@apple.com>
Mon, 5 Dec 2016 16:28:47 +0000 (16:28 +0000)
commitab139a9e7998ee29d8d2254230811909d5957814
tree5346c5ffba71914a8de49a345bf7fd1cdd115921
parent1f158d6955a31b4decf03c5bf2d568f5e29cc2e8
[analyzer] ObjCGenerics: Warn only on mismatch for invariant type parameters

On a method call, the ObjCGenerics checker uses the type tracked by
DynamicTypePropagation for the receiver to to infer substituted parmeter types
for the called methods and warns when the argument type does not match the
parameter.

Unfortunately, using the tracked type can result in false positives when the
receiver has a non-invariant type parameter and has been intentionally upcast.
For example, becaue NSArray's type parameter is covaraint, the following code
is perfectly safe:

NSArray<NSString *> *allStrings = ...
NSDate *date = ...;
NSArray<NSObject *> *allObjects = allStrings;
NSArray<NSObject *> *moreObjects = [allObjects arrayByAddingObject:date];

but the checker currently warns that the date parameter is not an NSString *.

To avoid this kind of false positive, the checker will now only warn when
the class defining the called method has only invariant type parameters.

rdar://problem/28803951

llvm-svn: 288677
clang/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
clang/test/Analysis/generics.m