@@ -8559,6 +8559,36 @@ static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr,
8559
8559
<< LHSExpr->getSourceRange () << RHSExpr->getSourceRange ();
8560
8560
}
8561
8561
8562
+ // / Check if a bitwise-& is performed on an Objective-C pointer. This
8563
+ // / is usually indicative of introspection within the Objective-C pointer.
8564
+ static void checkObjCPointerIntrospection (Sema &S, ExprResult &L, ExprResult &R,
8565
+ SourceLocation OpLoc) {
8566
+ if (!S.getLangOpts ().ObjC1 )
8567
+ return ;
8568
+
8569
+ const Expr *ObjCPointerExpr = 0 , *OtherExpr = 0 ;
8570
+ const Expr *LHS = L.get ();
8571
+ const Expr *RHS = R.get ();
8572
+
8573
+ if (LHS->IgnoreParenCasts ()->getType ()->isObjCObjectPointerType ()) {
8574
+ ObjCPointerExpr = LHS;
8575
+ OtherExpr = RHS;
8576
+ }
8577
+ else if (RHS->IgnoreParenCasts ()->getType ()->isObjCObjectPointerType ()) {
8578
+ ObjCPointerExpr = RHS;
8579
+ OtherExpr = LHS;
8580
+ }
8581
+
8582
+ // This warning is deliberately made very specific to reduce false
8583
+ // positives with logic that uses '&' for hashing. This logic mainly
8584
+ // looks for code trying to introspect into tagged pointers, which
8585
+ // code should generally never do.
8586
+ if (ObjCPointerExpr && isa<IntegerLiteral>(OtherExpr->IgnoreParenCasts ())) {
8587
+ S.Diag (OpLoc, diag::warn_objc_pointer_masking)
8588
+ << ObjCPointerExpr->getSourceRange ();
8589
+ }
8590
+ }
8591
+
8562
8592
// / CreateBuiltinBinOp - Creates a new built-in binary operation with
8563
8593
// / operator @p Opc at location @c TokLoc. This routine only supports
8564
8594
// / built-in operations; ActOnBinOp handles overloaded operators.
@@ -8636,6 +8666,7 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
8636
8666
ResultTy = CheckCompareOperands (LHS, RHS, OpLoc, Opc, false );
8637
8667
break ;
8638
8668
case BO_And:
8669
+ checkObjCPointerIntrospection (*this , LHS, RHS, OpLoc);
8639
8670
case BO_Xor:
8640
8671
case BO_Or:
8641
8672
ResultTy = CheckBitwiseOperands (LHS, RHS, OpLoc);
0 commit comments