[cfe-commits] r48807 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/format-attribute.c

Nuno Lopes nunoplopes at sapo.pt
Tue Mar 25 16:01:48 PDT 2008


Author: nlopes
Date: Tue Mar 25 18:01:48 2008
New Revision: 48807

URL: http://llvm.org/viewvc/llvm-project?rev=48807&view=rev
Log:
allow the format attribute to be specified in function pointer prototypes
# this is my first commit here, so please be gentle :)

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/format-attribute.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=48807&r1=48806&r2=48807&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Mar 25 18:01:48 2008
@@ -2136,6 +2136,24 @@
   d->addAttr(new NoThrowAttr());
 }
 
+static const FunctionTypeProto *getFunctionProto(Decl *d) {
+  ValueDecl *decl = dyn_cast<ValueDecl>(d);
+  if (!decl) return 0;
+
+  QualType Ty = decl->getType();
+
+  if (Ty->isFunctionPointerType()) {
+    const PointerType *PtrTy = Ty->getAsPointerType();
+    Ty = PtrTy->getPointeeType();
+  }
+
+  if (const FunctionType *FnTy = Ty->getAsFunctionType())
+    return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
+
+  return 0;
+}
+
+
 /// Handle __attribute__((format(type,idx,firstarg))) attributes
 /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
 void Sema::HandleFormatAttribute(Decl *d, AttributeList *rawAttr) {
@@ -2152,22 +2170,20 @@
     return;
   }
 
-  FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
-  if (!Fn) {
+  // GCC ignores the format attribute on K&R style function
+  // prototypes, so we ignore it as well
+  const FunctionTypeProto *proto = getFunctionProto(d);
+
+  if (!proto) {
     Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
            "format", "function");
     return;
   }
 
-  const FunctionTypeProto *proto =
-      dyn_cast<FunctionTypeProto>(Fn->getType()->getAsFunctionType());
-  if (!proto)
-    return;
-
   // 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  = Fn->getNumParams();
+  unsigned NumArgs  = proto->getNumArgs();
   unsigned FirstIdx = 1;
 
   const char *Format = rawAttr->getParameterName()->getName();

Modified: cfe/trunk/test/Sema/format-attribute.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-attribute.c?rev=48807&r1=48806&r2=48807&view=diff

==============================================================================
--- cfe/trunk/test/Sema/format-attribute.c (original)
+++ cfe/trunk/test/Sema/format-attribute.c Tue Mar 25 18:01:48 2008
@@ -14,3 +14,6 @@
 
 void y(char *str) __attribute__((format(strftime, 1,0))); // no-error
 void z(char *str, int c, ...) __attribute__((format(strftime, 1,2))); // expected-error {{strftime format attribute requires 3rd parameter to be 0}}
+
+int (*f_ptr)(char*,...) __attribute__((format(printf, 1,2))); // no-error
+int (*f2_ptr)(double,...) __attribute__((format(printf, 1, 2))); // expected-error {{format argument not a string type}}





More information about the cfe-commits mailing list