]> granicus.if.org Git - clang/commitdiff
Fix PR8625 and correctly interpret member-calls to static members when
authorChandler Carruth <chandlerc@gmail.com>
Tue, 16 Nov 2010 08:49:43 +0000 (08:49 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Tue, 16 Nov 2010 08:49:43 +0000 (08:49 +0000)
producing warnings.

This feels really fragile, and I've not audited all other argument index-based
warnings. I suspect we'll grow this bug on another warning eventually. It might
be nice to adjust the argument indices when building up the attribute AST node,
as we already have to remember about the 'this' argument within that code to
produce correct errors.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119340 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/attr-format.cpp

index 83d7c0760ef966cdba0562cf5a5ee6b0c292e1f1..bce28c2c91d854a2d16413ca12d467c9b30c968a 100644 (file)
@@ -1090,12 +1090,16 @@ Sema::CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg,
   // of member functions is counted. However, it doesn't appear in our own
   // lists, so decrement format_idx in that case.
   if (isa<CXXMemberCallExpr>(TheCall)) {
-    // Catch a format attribute mistakenly referring to the object argument.
-    if (format_idx == 0)
-      return;
-    --format_idx;
-    if(firstDataArg != 0)
-      --firstDataArg;
+    const CXXMethodDecl *method_decl =
+      dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl());
+    if (method_decl && method_decl->isInstance()) {
+      // Catch a format attribute mistakenly referring to the object argument.
+      if (format_idx == 0)
+        return;
+      --format_idx;
+      if(firstDataArg != 0)
+        --firstDataArg;
+    }
   }
 
   // CHECK: printf/scanf-like function is called with no format string.
index bf131496007b1dc5787c1aed758533b1f743af29..da134a136d26d081300cbba67f7a6ac2cace3bc3 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wformat-nonliteral -verify %s
 struct S {
   static void f(const char*, ...) __attribute__((format(printf, 1, 2)));
   static const char* f2(const char*) __attribute__((format_arg(1)));
@@ -21,3 +21,15 @@ struct A { void a(const char*,...) __attribute((format(printf,2,3))); };
 void b(A x) {
   x.a("%d", 3);
 }
+
+// PR8625: correctly interpret static member calls as not having an implicit
+// 'this' argument.
+namespace PR8625 {
+  struct S {
+    static void f(const char*, const char*, ...)
+      __attribute__((format(printf, 2, 3)));
+  };
+  void test(S s, const char* str) {
+    s.f(str, "%s", str);
+  }
+}