]> granicus.if.org Git - clang/commitdiff
-Wformat: Don't check format strings in uninstantiated templates.
authorJordan Rose <jordan_rose@apple.com>
Tue, 2 Oct 2012 01:49:54 +0000 (01:49 +0000)
committerJordan Rose <jordan_rose@apple.com>
Tue, 2 Oct 2012 01:49:54 +0000 (01:49 +0000)
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>

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

lib/Sema/SemaChecking.cpp
test/SemaCXX/format-strings.cpp
test/SemaObjCXX/format-strings.mm [new file with mode: 0644]

index ebb6cd10652d34f09d7ef8b55b50f3c7894049ab..7498d348920404ae6db437921c8ae3a74bdb5666 100644 (file)
@@ -495,9 +495,8 @@ void Sema::checkCall(NamedDecl *FDecl, Expr **Args,
                      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;
index 6b0df2935378fb85c9cec61bff0b10fc751436f9..299aa81bb1612951f3b81895c693c432f1425355 100644 (file)
@@ -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 @@ int Foo::printf2(const char *fmt, ...) {
 
   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}}
+  }
+}
+
diff --git a/test/SemaObjCXX/format-strings.mm b/test/SemaObjCXX/format-strings.mm
new file mode 100644 (file)
index 0000000..2fb92e2
--- /dev/null
@@ -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);
+}
+
+@class NSString;
+
+@interface Format
++ (void)print:(NSString *)format, ... __attribute__((format(NSString, 1, 2)));
+@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}}
+  }
+}
+