Skip to content

Commit ebeabab

Browse files
committed
Add a warning for Objective-C pointer introspection, which is solely the job of the Objective-C runtime.
llvm-svn: 180062
1 parent a53cd7e commit ebeabab

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ def warn_objc_isa_use : Warning<
642642
def warn_objc_isa_assign : Warning<
643643
"assignment to Objective-C's isa is deprecated in favor of "
644644
"object_setClass()">, InGroup<DeprecatedObjCIsaUsage>;
645+
def warn_objc_pointer_masking : Warning<
646+
"bitmasking for introspection of Objective-C object pointers is strongly "
647+
"discouraged in favor of using runtime APIs">,
648+
InGroup<DiagGroup<"deprecated-objc-pointer-introspection">>;
645649
def warn_objc_property_default_assign_on_object : Warning<
646650
"default property attribute 'assign' not appropriate for non-GC object">,
647651
InGroup<ObjCPropertyNoAttribute>;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8559,6 +8559,36 @@ static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr,
85598559
<< LHSExpr->getSourceRange() << RHSExpr->getSourceRange();
85608560
}
85618561

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+
85628592
/// CreateBuiltinBinOp - Creates a new built-in binary operation with
85638593
/// operator @p Opc at location @c TokLoc. This routine only supports
85648594
/// built-in operations; ActOnBinOp handles overloaded operators.
@@ -8636,6 +8666,7 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
86368666
ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false);
86378667
break;
86388668
case BO_And:
8669+
checkObjCPointerIntrospection(*this, LHS, RHS, OpLoc);
86398670
case BO_Xor:
86408671
case BO_Or:
86418672
ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc);

0 commit comments

Comments
 (0)