diff options
author | Jordan Rose <jordan_rose@apple.com> | 2012-07-17 17:46:48 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2012-07-17 17:46:48 +0000 |
commit | d5209ae13a7c42e2b7fa641f75a66e545959cbed (patch) | |
tree | 980312ff8d6ec328ad4b415ebea8adf638b6d299 /lib/Sema/SemaExpr.cpp | |
parent | eec207f02a0208814bb8b4ca90099a2fed016244 (diff) |
-Wobjc-literal-compare: don't warn when comparing against nil.
Checks against nil often appear as guards in macros, and comparing
Objective-C literals to nil has well-defined behavior (if tautological).
On OS X, 'nil' has not been typed as 'id' since 10.6 (possibly earlier),
so the warning was already not firing, but other runtimes continue to use
((id)0) or some variant. This change accepts comparisons to any null pointer;
to keep it simple, it looks through all casts (not just casts to 'id').
PR13276
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160379 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaExpr.cpp')
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index fabd673d1b..34fb9d06a7 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -6742,10 +6742,24 @@ static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, ExprResult &RHS, BinaryOperator::Opcode Opc){ - Expr *Literal = (isObjCObjectLiteral(LHS) ? LHS : RHS).get(); + Expr *Literal; + Expr *Other; + if (isObjCObjectLiteral(LHS)) { + Literal = LHS.get(); + Other = RHS.get(); + } else { + Literal = RHS.get(); + Other = LHS.get(); + } + + // Don't warn on comparisons against nil. + Other = Other->IgnoreParenCasts(); + if (Other->isNullPointerConstant(S.getASTContext(), + Expr::NPC_ValueDependentIsNotNull)) + return; // This should be kept in sync with warn_objc_literal_comparison. - // LK_String should always be last, since it has its own flag. + // LK_String should always be last, since it has its own warning flag. enum { LK_Array, LK_Dictionary, |