[PATCH][ObjC] Cleanup ObjCInterfaceDecl lookup for ObjC literals

John McCall rjmccall at apple.com
Tue Jul 14 11:53:24 PDT 2015


> On Jul 12, 2015, at 12:53 PM, AlexDenisov <1101.debian at gmail.com> wrote:
> 
> ObjC literals (NSDictionary, NSArray, Boxed Expressions) use the same
> code to instantiate ObjCInterfaceDecl of corresponding classes.
> 
> The patch extracts duplicated code into one method and unifies
> validation of the ObjCInterfaceDecl’s.
> 
> Also, diagnostic for NSDictionary/NSArray was changed a bit.
> Before:
> 
> @class NSDictionary;
> // …
> id dictionary = @{}; // expected-error {{declaration of 'dictionaryWithObjects:forKeys:count:' is missing in NSDictionary class}}
> 
> After:
> 
> @class NSDictionary; // expected-note {{forward declaration of class here}}
> // …
> id dictionary = @{}; // expected-error {{NSDictionary must be available to use Objective-C dictionary literals}}

Hmm.  This is a lot better, but “available” is still a bit imprecise; it would be better if this were just slightly more explicit:
  error: definition of class NSDictionary must be available to use Objective-C dictionary literals

+/// \brief Validates ObjCInterfaceDecl availability.
+/// ObjCInterfaceDecl, used to create ObjC literals, should be defined
+/// if clang not in a debugger mode.
+static bool ValidateObjCLiteralInterfaceDecl(Sema &S, ObjCInterfaceDecl *Decl,
+                                          SourceLocation Loc, unsigned DiagID) {
+  if (!Decl) {
+    S.Diag(Loc, DiagID);
+    return false;
+  } else if (!Decl->hasDefinition() && !S.getLangOpts().DebuggerObjCLiteral) {
+    S.Diag(Loc, DiagID);
+    S.Diag(Decl->getLocation(), diag::note_forward_class);
+    return false;
+  }

For the benefit of people using non-standard NSAPI classes, please also
change this diagnostic so that the class name is a parameter.  Something like:

def err_undeclared_objc_literal_class : Error<
  “definition of class %0 must be available to use Objective-C “
  "%select{array|dictionary|…}1 literals">;

Then, on the Sema side, you should define an enum that corresponds to
the different cases in that second parameter.

If you do this, you should be able to unify all the different diagnostics for
NSNumber/NSArray/NSValue.

The rest of the patch looks good.

John.



More information about the cfe-commits mailing list