[cfe-commits] r79987 - in /cfe/trunk: lib/Sema/SemaDeclAttr.cpp test/SemaCXX/attr-format.cpp

Anders Carlsson andersca at mac.com
Tue Aug 25 07:12:34 PDT 2009


Author: andersca
Date: Tue Aug 25 09:12:34 2009
New Revision: 79987

URL: http://llvm.org/viewvc/llvm-project?rev=79987&view=rev
Log:
Handle the implicit 'this' parameter for format attributes.

Added:
    cfe/trunk/test/SemaCXX/attr-format.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Tue Aug 25 09:12:34 2009
@@ -1222,9 +1222,6 @@
     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  = getFunctionOrMethodNumArgs(d);
   unsigned FirstIdx = 1;
 
@@ -1271,6 +1268,16 @@
     return;
   }
 
+  // FIXME: We should handle the implicit 'this' parameter in a more generic
+  // way that can be used for other arguments.
+  bool HasImplicitThisParam = false;
+  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
+    if (MD->isInstance()) {
+      HasImplicitThisParam = true;
+      NumArgs++;
+    }
+  }
+  
   if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
     S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
       << "format" << 2 << IdxExpr->getSourceRange();
@@ -1280,6 +1287,8 @@
   // FIXME: Do we need to bounds check?
   unsigned ArgIdx = Idx.getZExtValue() - 1;
 
+  if (HasImplicitThisParam) ArgIdx--;
+  
   // make sure the format string is really a string
   QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
 

Added: cfe/trunk/test/SemaCXX/attr-format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-format.cpp?rev=79987&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/attr-format.cpp (added)
+++ cfe/trunk/test/SemaCXX/attr-format.cpp Tue Aug 25 09:12:34 2009
@@ -0,0 +1,8 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+struct S {
+	static void f(const char*, ...) __attribute__((format(printf, 1, 2)));
+	
+	// GCC has a hidden 'this' argument in member functions which is why
+	// the format argument is argument 2 here.
+	void g(const char*, ...) __attribute__((format(printf, 2, 3)));
+};
\ No newline at end of file





More information about the cfe-commits mailing list