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

Jordan Rose jordan_rose at apple.com
Mon Oct 1 18:49:54 PDT 2012


Author: jrose
Date: Mon Oct  1 20:49:54 2012
New Revision: 164984

URL: http://llvm.org/viewvc/llvm-project?rev=164984&view=rev
Log:
-Wformat: Don't check format strings in uninstantiated templates.

Also applies to -Wnonnull, -Wtype-safety, and -Wnon-pod-varargs.
All of these can be better checked at instantiation time.

This change does not actually affect regular CallExpr function calls,
since the checks there only happen after overload resolution.
However, it will affect Objective-C method calls.

<rdar://problem/12373934>

Added:
    cfe/trunk/test/SemaObjCXX/format-strings.mm
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=164984&r1=164983&r2=164984&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Oct  1 20:49:54 2012
@@ -495,9 +495,8 @@
                      SourceLocation Loc,
                      SourceRange Range,
                      VariadicCallType CallType) {
-  // FIXME: This mechanism should be abstracted to be less fragile and
-  // more efficient. For example, just map function ids to custom
-  // handlers.
+  if (CurContext->isDependentContext())
+    return;
 
   // Printf and scanf checking.
   bool HandledFormatString = false;

Modified: cfe/trunk/test/SemaCXX/format-strings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/format-strings.cpp?rev=164984&r1=164983&r2=164984&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/format-strings.cpp (original)
+++ cfe/trunk/test/SemaCXX/format-strings.cpp Mon Oct  1 20:49:54 2012
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic -fblocks %s
 
 #include <stdarg.h>
 
@@ -75,3 +75,61 @@
 
   return 0;
 }
+
+
+namespace Templates {
+  template<typename T>
+  void my_uninstantiated_print(const T &arg) {
+    printf("%d", arg); // no-warning
+  }
+
+  template<typename T>
+  void my_print(const T &arg) {
+    printf("%d", arg); // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
+  }
+
+  void use_my_print() {
+    my_print("abc"); // expected-note {{requested here}}
+  }
+
+
+  template<typename T>
+  class UninstantiatedPrinter {
+  public:
+    static void print(const T &arg) {
+      printf("%d", arg); // no-warning
+    }
+  };
+
+  template<typename T>
+  class Printer {
+    void format(const char *fmt, ...) __attribute__((format(printf,2,3)));
+  public:
+
+    void print(const T &arg) {
+      format("%d", arg); // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
+    }
+  };
+
+  void use_class(Printer<const char *> &p) {
+    p.print("abc"); // expected-note {{requested here}}
+  }
+
+  
+  extern void (^block_print)(const char * format, ...) __attribute__((format(printf, 1, 2)));
+
+  template<typename T>
+  void uninstantiated_call_block_print(const T &arg) {
+    block_print("%d", arg); // no-warning
+  }
+
+  template<typename T>
+  void call_block_print(const T &arg) {
+    block_print("%d", arg); // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
+  }
+
+  void use_block_print() {
+    call_block_print("abc"); // expected-note {{requested here}}
+  }
+}
+

Added: cfe/trunk/test/SemaObjCXX/format-strings.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/format-strings.mm?rev=164984&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjCXX/format-strings.mm (added)
+++ cfe/trunk/test/SemaObjCXX/format-strings.mm Mon Oct  1 20:49:54 2012
@@ -0,0 +1,81 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s
+
+#include <stdarg.h>
+
+extern "C" {
+extern int scanf(const char *restrict, ...);
+extern int printf(const char *restrict, ...);
+extern int vprintf(const char *restrict, va_list);
+}
+
+ at class NSString;
+
+ at interface Format
++ (void)print:(NSString *)format, ... __attribute__((format(NSString, 1, 2)));
+ at end
+
+
+namespace Templates {
+  template<typename T>
+  void my_uninstantiated_print(const T &arg) {
+    [Format print:@"%d", arg];
+  }
+
+  template<typename T>
+  void my_print(const T &arg) {
+    [Format print:@"%d", arg]; // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
+  }
+
+  void use_my_print() {
+    my_print("abc"); // expected-note {{requested here}}
+  }
+
+
+  template<typename T>
+  class UninstantiatedPrinter {
+  public:
+    static void print(const T &arg) {
+      [Format print:@"%d", arg]; // no-warning
+    }
+  };
+
+  template<typename T>
+  class Printer {
+  public:
+    void print(const T &arg) {
+      [Format print:@"%d", arg]; // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
+    }
+  };
+
+  void use_class(Printer<const char *> &p) {
+    p.print("abc"); // expected-note {{requested here}}
+  }
+
+
+  template<typename T>
+  class UninstantiatedWrapper {
+  public:
+    class Printer {
+    public:
+      void print(const T &arg) {
+        [Format print:@"%d", arg]; // no-warning
+      }
+    };
+  };
+
+  template<typename T>
+  class Wrapper {
+  public:
+    class Printer {
+    public:
+      void print(const T &arg) {
+        [Format print:@"%d", arg]; // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
+      }
+    };
+  };
+
+  void use_class(Wrapper<const char *>::Printer &p) {
+    p.print("abc"); // expected-note {{requested here}}
+  }
+}
+





More information about the cfe-commits mailing list