r194915 - ObjetiveC ARC. Start diagnosing invalid toll free bridging.
Douglas Gregor
dgregor at apple.com
Mon Nov 18 16:20:34 PST 2013
On Nov 15, 2013, at 5:45 PM, Fariborz Jahanian <fjahanian at apple.com> wrote:
> Author: fjahanian
> Date: Fri Nov 15 19:45:25 2013
> New Revision: 194915
>
> URL: http://llvm.org/viewvc/llvm-project?rev=194915&view=rev
> Log:
> ObjetiveC ARC. Start diagnosing invalid toll free bridging.
> // rdar://15454846.
>
> Modified:
> cfe/trunk/include/clang/Basic/DiagnosticGroups.td
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> cfe/trunk/lib/Sema/SemaExprObjC.cpp
> cfe/trunk/test/SemaObjC/objcbridge-attribute.m
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=194915&r1=194914&r2=194915&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Nov 15 19:45:25 2013
> @@ -381,6 +381,8 @@ def AutomaticReferenceCounting : DiagGro
> def ARCRepeatedUseOfWeakMaybe : DiagGroup<"arc-maybe-repeated-use-of-weak">;
> def ARCRepeatedUseOfWeak : DiagGroup<"arc-repeated-use-of-weak",
> [ARCRepeatedUseOfWeakMaybe]>;
> +def ObjCBridge : DiagGroup<"arc-bridge-cast">;
> +
These toll-free bridging annotations don’t actually have anything to do with ARC. The annotations and warnings are just as valid in non-ARC. The only ARC-specific consider I have is to make sure we’re providing the warnings for both normal C-style casts and the ARC bridging casts (e.g., (__bridge NSString *)cfstr). It’s an unfortunate accident of history that both are called “bridging” that caused the confusion :(
> def SelectorTypeMismatch : DiagGroup<"selector-type-mismatch">;
> def Selector : DiagGroup<"selector", [SelectorTypeMismatch]>;
> def Protocol : DiagGroup<"protocol">;
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=194915&r1=194914&r2=194915&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Nov 15 19:45:25 2013
> @@ -2449,6 +2449,8 @@ def err_objc_bridge_not_pointert_to_stru
> "'objc_bridge' attribute must be applied to a pointer to struct type">;
> def err_objc_bridged_not_interface : Error<
> "CF object of type %0 is bridged to '%1', which is not an Objective-C class">;
> +def warn_objc_invalid_bridge : Warning<
> + "%0 bridges to %1, not %2">, InGroup<ObjCBridge>;
>
> // Function Parameter Semantic Analysis.
> def err_param_with_void_type : Error<"argument may not have 'void' type">;
>
> Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=194915&r1=194914&r2=194915&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Fri Nov 15 19:45:25 2013
> @@ -3172,23 +3172,32 @@ static bool CheckObjCBridgeCast(Sema &S,
> if (TDNDecl->hasAttr<ObjCBridgeAttr>()) {
> ObjCBridgeAttr *ObjCBAttr = TDNDecl->getAttr<ObjCBridgeAttr>();
> IdentifierInfo *Parm = ObjCBAttr->getBridgedType();
> + NamedDecl *Target = 0;
> if (Parm && S.getLangOpts().ObjC1) {
> // Check for an existing type with this name.
> LookupResult R(S, DeclarationName(Parm), SourceLocation(),
> Sema::LookupOrdinaryName);
> if (S.LookupName(R, S.TUScope)) {
> - NamedDecl *Target = R.getFoundDecl();
> - if (Target && !isa<ObjCInterfaceDecl>(Target)) {
> - S.Diag(castExpr->getLocStart(), diag::err_objc_bridged_not_interface)
> - << castExpr->getType() << Parm->getName();
> - S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
> - S.Diag(Target->getLocStart(), diag::note_declared_at);
> + Target = R.getFoundDecl();
> + if (Target && isa<ObjCInterfaceDecl>(Target)) {
> + ObjCInterfaceDecl *ExprClass = cast<ObjCInterfaceDecl>(Target);
> + if (const ObjCObjectPointerType *InterfacePointerType =
> + castType->getAsObjCInterfacePointerType()) {
> + ObjCInterfaceDecl *CastClass
> + = InterfacePointerType->getObjectType()->getInterface();
> + if ((CastClass == ExprClass) || (CastClass && ExprClass->isSuperClassOf(CastClass)))
> + return true;
> + S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge)
> + << TDNDecl->getName() << Target->getName() << CastClass->getName();
Please pass the actual QualTypes to the diagnostic rather than the StringRefs from getName(). This will make sure the types are properly quoted and desugared in the diagnostic.
- Doug
> + return true;
> + }
> }
> - } else {
> - S.Diag(castExpr->getLocStart(), diag::err_objc_bridged_not_interface)
> - << castExpr->getType() << Parm->getName();
> - S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
> }
> + S.Diag(castExpr->getLocStart(), diag::err_objc_bridged_not_interface)
> + << castExpr->getType() << Parm->getName();
> + S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
> + if (Target)
> + S.Diag(Target->getLocStart(), diag::note_declared_at);
> }
> return true;
> }
>
> Modified: cfe/trunk/test/SemaObjC/objcbridge-attribute.m
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objcbridge-attribute.m?rev=194915&r1=194914&r2=194915&view=diff
> ==============================================================================
> --- cfe/trunk/test/SemaObjC/objcbridge-attribute.m (original)
> +++ cfe/trunk/test/SemaObjC/objcbridge-attribute.m Fri Nov 15 19:45:25 2013
> @@ -48,6 +48,8 @@ typedef CFErrorRef1 CFErrorRef2;
>
> @class NSString;
>
> -id Test2(CFErrorRef2 cf) {
> - return (NSString *)cf;
> +void Test2(CFErrorRef2 cf) {
> + (void)(NSString *)cf; // expected-warning {{CFErrorRef bridges to NSError, not NSString}}
> + (void)(NSError *)cf; // okay
> + (void)(MyError*)cf; // okay,
> }
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list