Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s
      2 
      3 #include <stdarg.h>
      4 
      5 extern "C" {
      6 extern int scanf(const char *restrict, ...);
      7 extern int printf(const char *restrict, ...);
      8 extern int vprintf(const char *restrict, va_list);
      9 }
     10 
     11 void f(char **sp, float *fp) {
     12   scanf("%as", sp); // expected-warning{{'a' length modifier is not supported by ISO C}}
     13 
     14   // TODO: Warn that the 'a' conversion specifier is a C++11 feature.
     15   printf("%a", 1.0);
     16   scanf("%afoobar", fp);
     17 }
     18 
     19 void g() {
     20   printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}}
     21 }
     22 
     23 // Test that we properly handle format_idx on C++ members.
     24 class Foo {
     25 public:
     26   const char *gettext(const char *fmt) __attribute__((format_arg(2)));
     27 
     28   int scanf(const char *, ...) __attribute__((format(scanf, 2, 3)));
     29   int printf(const char *, ...) __attribute__((format(printf, 2, 3)));
     30   int printf2(const char *, ...);
     31 
     32   static const char *gettext_static(const char *fmt) __attribute__((format_arg(1)));
     33   static int printf_static(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
     34 };
     35 
     36 void h(int *i) {
     37   Foo foo;
     38   foo.scanf("%d"); // expected-warning{{more '%' conversions than data arguments}}
     39   foo.printf("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
     40   Foo::printf_static("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
     41 
     42   printf(foo.gettext("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
     43   printf(Foo::gettext_static("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
     44 }
     45 
     46 // Test handling __null for format string literal checking.
     47 extern "C" {
     48   int test_null_format(const char *format, ...) __attribute__((__format__ (__printf__, 1, 2)));
     49 }
     50 
     51 void rdar8269537(const char *f)
     52 {
     53   test_null_format(false); // expected-warning {{null from a constant boolean}}
     54   test_null_format(0); // no-warning
     55   test_null_format(__null); // no-warning
     56   test_null_format(f); // expected-warning {{not a string literal}}
     57 }
     58 
     59 int Foo::printf(const char *fmt, ...) {
     60   va_list ap;
     61   va_start(ap,fmt);
     62   const char * const format = fmt;
     63   vprintf(format, ap); // no-warning
     64 
     65   const char *format2 = fmt;
     66   vprintf(format2, ap); // expected-warning{{format string is not a string literal}}
     67 
     68   return 0;
     69 }
     70 
     71 int Foo::printf2(const char *fmt, ...) {
     72   va_list ap;
     73   va_start(ap,fmt);
     74   vprintf(fmt, ap); // expected-warning{{format string is not a string literal}}
     75 
     76   return 0;
     77 }
     78