[cfe-commits] r149998 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/SemaCXX/format-strings.cpp

Jean-Daniel Dupas devlists at shadowlab.org
Tue Feb 7 11:01:42 PST 2012


Author: jddupas
Date: Tue Feb  7 13:01:42 2012
New Revision: 149998

URL: http://llvm.org/viewvc/llvm-project?rev=149998&view=rev
Log:
Implements support of format_arg attribute on C++ member.


Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/format-strings.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=149998&r1=149997&r2=149998&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Feb  7 13:01:42 2012
@@ -1459,21 +1459,20 @@
     return false;
   }
 
-  case Stmt::CallExprClass: {
+  case Stmt::CallExprClass:
+  case Stmt::CXXMemberCallExprClass: {
     const CallExpr *CE = cast<CallExpr>(E);
-    if (const ImplicitCastExpr *ICE
-          = dyn_cast<ImplicitCastExpr>(CE->getCallee())) {
-      if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
-        if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
-          if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) {
-            unsigned ArgIndex = FA->getFormatIdx();
-            const Expr *Arg = CE->getArg(ArgIndex - 1);
-
-            return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg,
-                                          format_idx, firstDataArg, Type,
-                                          inFunctionCall);
-          }
-        }
+    if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
+      if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
+        unsigned ArgIndex = FA->getFormatIdx();
+        if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
+          if (MD->isInstance())
+            --ArgIndex;
+        const Expr *Arg = CE->getArg(ArgIndex - 1);
+
+        return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg,
+                                      format_idx, firstDataArg, Type,
+                                      inFunctionCall);
       }
     }
 
@@ -1534,11 +1533,7 @@
   // The way the format attribute works in GCC, the implicit this argument
   // 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)) {
-    const CXXMethodDecl *method_decl = 
-    dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl());
-    IsCXXMember = method_decl && method_decl->isInstance();
-  }
+  IsCXXMember = isa<CXXMemberCallExpr>(TheCall);
   CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(),
                        IsCXXMember, TheCall->getRParenLoc(), 
                        TheCall->getCallee()->getSourceRange());

Modified: cfe/trunk/test/SemaCXX/format-strings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/format-strings.cpp?rev=149998&r1=149997&r2=149998&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/format-strings.cpp (original)
+++ cfe/trunk/test/SemaCXX/format-strings.cpp Tue Feb  7 13:01:42 2012
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s
 
 extern "C" {
 extern int scanf(const char *restrict, ...);
@@ -17,3 +17,25 @@
 void g() {
   printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}}
 }
+
+// Test that we properly handle format_idx on C++ members.
+class Foo {
+public:
+  const char *gettext(const char *fmt) __attribute__((format_arg(2)));
+
+  int scanf(const char *restrict, ...) __attribute__((format(scanf, 2, 3)));
+  int printf(const char *restrict, ...) __attribute__((format(printf, 2, 3)));
+
+  static const char *gettext_static(const char *fmt) __attribute__((format_arg(1)));
+  static int printf_static(const char *restrict, ...) __attribute__((format(printf, 1, 2)));
+};
+
+void h(int *i) {
+  Foo foo;
+  foo.scanf("%d"); // expected-warning{{more '%' conversions than data arguments}}
+  foo.printf("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
+  Foo::printf_static("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
+
+  printf(foo.gettext("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
+  printf(Foo::gettext_static("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
+}





More information about the cfe-commits mailing list