diff options
| author | Ted Kremenek <kremenek@apple.com> | 2013-04-22 22:46:52 +0000 |
|---|---|---|
| committer | Ted Kremenek <kremenek@apple.com> | 2013-04-22 22:46:52 +0000 |
| commit | ebeabab9a9b61b34439fbc243f4e1aab640440b6 (patch) | |
| tree | 5aa60144a43212925675320cf9a2dca9189890ab /clang/lib/Sema/SemaExpr.cpp | |
| parent | a53cd7e6ce3787c71e3cbaddc218892292ea54bd (diff) | |
| download | bcm5719-llvm-ebeabab9a9b61b34439fbc243f4e1aab640440b6.tar.gz bcm5719-llvm-ebeabab9a9b61b34439fbc243f4e1aab640440b6.zip | |
Add a warning for Objective-C pointer introspection, which is solely the job of the Objective-C runtime.
llvm-svn: 180062
Diffstat (limited to 'clang/lib/Sema/SemaExpr.cpp')
| -rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index fb0e9ef687c..a0e6c2144eb 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -8559,6 +8559,36 @@ static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); } +/// Check if a bitwise-& is performed on an Objective-C pointer. This +/// is usually indicative of introspection within the Objective-C pointer. +static void checkObjCPointerIntrospection(Sema &S, ExprResult &L, ExprResult &R, + SourceLocation OpLoc) { + if (!S.getLangOpts().ObjC1) + return; + + const Expr *ObjCPointerExpr = 0, *OtherExpr = 0; + const Expr *LHS = L.get(); + const Expr *RHS = R.get(); + + if (LHS->IgnoreParenCasts()->getType()->isObjCObjectPointerType()) { + ObjCPointerExpr = LHS; + OtherExpr = RHS; + } + else if (RHS->IgnoreParenCasts()->getType()->isObjCObjectPointerType()) { + ObjCPointerExpr = RHS; + OtherExpr = LHS; + } + + // This warning is deliberately made very specific to reduce false + // positives with logic that uses '&' for hashing. This logic mainly + // looks for code trying to introspect into tagged pointers, which + // code should generally never do. + if (ObjCPointerExpr && isa<IntegerLiteral>(OtherExpr->IgnoreParenCasts())) { + S.Diag(OpLoc, diag::warn_objc_pointer_masking) + << ObjCPointerExpr->getSourceRange(); + } +} + /// CreateBuiltinBinOp - Creates a new built-in binary operation with /// operator @p Opc at location @c TokLoc. This routine only supports /// built-in operations; ActOnBinOp handles overloaded operators. @@ -8636,6 +8666,7 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); break; case BO_And: + checkObjCPointerIntrospection(*this, LHS, RHS, OpLoc); case BO_Xor: case BO_Or: ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |

