]> granicus.if.org Git - clang/commitdiff
Sema support for format and noreturn attributes on Objective-C methods.
authorDaniel Dunbar <daniel@zuster.org>
Fri, 26 Sep 2008 04:12:28 +0000 (04:12 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 26 Sep 2008 04:12:28 +0000 (04:12 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56640 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclAttr.cpp
lib/Sema/SemaDeclObjC.cpp
test/SemaObjC/method-attributes.m [new file with mode: 0644]

index 764724aeca032f0830966787e294fe379c2cdd01..2bdb885f53587a685e19ced3a6187c2bd635813b 100644 (file)
@@ -45,6 +45,40 @@ static const FunctionTypeProto *getFunctionProto(Decl *d) {
   return 0;
 }
 
+// FIXME: We should provide an abstraction around a method or function
+// to provide the following bits of information.
+
+/// isFunctionOrMethod - Return true if the given decl is a (non-K&R)
+/// function or an Objective-C method.
+static bool isFunctionOrMethod(Decl *d) {
+  return getFunctionProto(d) || isa<ObjCMethodDecl>(d);
+
+}
+
+static unsigned getFunctionOrMethodNumArgs(Decl *d) {
+  if (const FunctionTypeProto *proto = getFunctionProto(d)) {
+    return proto->getNumArgs();
+  } else {
+    return cast<ObjCMethodDecl>(d)->getNumParams();
+  }
+}
+
+static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
+  if (const FunctionTypeProto *proto = getFunctionProto(d)) {
+    return proto->getArgType(Idx);
+  } else {
+    return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType();
+  }
+}
+
+static bool isFunctionOrMethodVariadic(Decl *d) {
+  if (const FunctionTypeProto *proto = getFunctionProto(d)) {
+    return proto->isVariadic();
+  } else {
+    return cast<ObjCMethodDecl>(d)->isVariadic();
+  }
+}
+
 static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
   const PointerType *PT = T->getAsPointerType();
   if (!PT)
@@ -360,8 +394,7 @@ static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
     return;
   }
   
-  FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
-  if (!Fn) {
+  if (!isa<FunctionDecl>(d) && !isa<ObjCMethodDecl>(d)) {
     S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
            "noreturn", "function");
     return;
@@ -637,9 +670,7 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
 
   // GCC ignores the format attribute on K&R style function
   // prototypes, so we ignore it as well
-  const FunctionTypeProto *proto = getFunctionProto(d);
-
-  if (!proto) {
+  if (!isFunctionOrMethod(d)) {
     S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
            "format", "function");
     return;
@@ -648,7 +679,7 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   // FIXME: in C++ the implicit 'this' function parameter also counts.
   // this is needed in order to be compatible with GCC
   // the index must start in 1 and the limit is numargs+1
-  unsigned NumArgs  = proto->getNumArgs();
+  unsigned NumArgs  = getFunctionOrMethodNumArgs(d);
   unsigned FirstIdx = 1;
 
   const char *Format = Attr.getParameterName()->getName();
@@ -703,7 +734,7 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   unsigned ArgIdx = Idx.getZExtValue() - 1;
   
   // make sure the format string is really a string
-  QualType Ty = proto->getArgType(ArgIdx);
+  QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
 
   if (is_CFString) {
     if (!isCFStringType(Ty, S.Context)) {
@@ -741,7 +772,7 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
 
   // check if the function is variadic if the 3rd argument non-zero
   if (FirstArg != 0) {
-    if (proto->isVariadic()) {
+    if (isFunctionOrMethodVariadic(d)) {
       ++NumArgs; // +1 for ...
     } else {
       S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
index dbb3cf22ad3d69e8e465ccaf6c43276bdd9b56e3..1b166c4094ef3722439996613fe1aed884cc2b4a 100644 (file)
@@ -978,8 +978,6 @@ Sema::DeclTy *Sema::ActOnMethodDeclaration(
                            MethodDeclKind == tok::objc_optional ? 
                            ObjCMethodDecl::Optional : 
                            ObjCMethodDecl::Required);
-  if (AttrList)
-    ProcessDeclAttributeList(ObjCMethod, AttrList);
   
   llvm::SmallVector<ParmVarDecl*, 16> Params;
   
@@ -1004,6 +1002,9 @@ Sema::DeclTy *Sema::ActOnMethodDeclaration(
   ObjCMethod->setObjCDeclQualifier(
     CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
   const ObjCMethodDecl *PrevMethod = 0;
+
+  if (AttrList)
+    ProcessDeclAttributeList(ObjCMethod, AttrList);
  
   // For implementations (which can be very "coarse grain"), we add the 
   // method now. This allows the AST to implement lookup methods that work 
diff --git a/test/SemaObjC/method-attributes.m b/test/SemaObjC/method-attributes.m
new file mode 100644 (file)
index 0000000..d16d6ff
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: clang -fsyntax-only %s
+
+@class NSString;
+
+@interface A
+-t1 __attribute__((noreturn));
+- (NSString *)stringByAppendingFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
+@end