]> granicus.if.org Git - clang/commitdiff
Handle the implicit 'this' parameter for format attributes.
authorAnders Carlsson <andersca@mac.com>
Tue, 25 Aug 2009 14:12:34 +0000 (14:12 +0000)
committerAnders Carlsson <andersca@mac.com>
Tue, 25 Aug 2009 14:12:34 +0000 (14:12 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79987 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclAttr.cpp
test/SemaCXX/attr-format.cpp [new file with mode: 0644]

index af0f03d2d9f083ec4730ae77aaefeb53fcd1e6c2..c6a5241f94dcabebddb098eb251f9ffaf7c41e0b 100644 (file)
@@ -1222,9 +1222,6 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
     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 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
     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 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   // 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);
 
diff --git a/test/SemaCXX/attr-format.cpp b/test/SemaCXX/attr-format.cpp
new file mode 100644 (file)
index 0000000..d1659c9
--- /dev/null
@@ -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