attr-format.c 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //RUN: %clang_cc1 -fsyntax-only -verify %s
  2. #include <stdarg.h>
  3. void a(const char *a, ...) __attribute__((format(printf, 1,2))); // no-error
  4. void b(const char *a, ...) __attribute__((format(printf, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
  5. void c(const char *a, ...) __attribute__((format(printf, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
  6. void d(const char *a, int c) __attribute__((format(printf, 1,2))); // expected-error {{format attribute requires variadic function}}
  7. void e(char *str, int c, ...) __attribute__((format(printf, 2,3))); // expected-error {{format argument not a string type}}
  8. typedef const char* xpto;
  9. void f(xpto c, va_list list) __attribute__((format(printf, 1, 0))); // no-error
  10. void g(xpto c) __attribute__((format(printf, 1, 0))); // no-error
  11. void y(char *str) __attribute__((format(strftime, 1,0))); // no-error
  12. void z(char *str, int c, ...) __attribute__((format(strftime, 1,2))); // expected-error {{strftime format attribute requires 3rd parameter to be 0}}
  13. int (*f_ptr)(char*,...) __attribute__((format(printf, 1,2))); // no-error
  14. int (*f2_ptr)(double,...) __attribute__((format(printf, 1, 2))); // expected-error {{format argument not a string type}}
  15. struct _mystruct {
  16. int (*printf)(const char *format, ...) __attribute__((__format__(printf, 1, 2))); // no-error
  17. int (*printf2)(double format, ...) __attribute__((__format__(printf, 1, 2))); // expected-error {{format argument not a string type}}
  18. };
  19. typedef int (*f3_ptr)(char*,...) __attribute__((format(printf,1,0))); // no-error
  20. // <rdar://problem/6623513>
  21. int rdar6623513(void *, const char*, const char*, ...)
  22. __attribute__ ((format (printf, 3, 0)));
  23. int rdar6623513_aux(int len, const char* s) {
  24. rdar6623513(0, "hello", "%.*s", len, s);
  25. }
  26. // same as format(printf(...))...
  27. void a2(const char *a, ...) __attribute__((format(printf0, 1,2))); // no-error
  28. void b2(const char *a, ...) __attribute__((format(printf0, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
  29. void c2(const char *a, ...) __attribute__((format(printf0, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
  30. void d2(const char *a, int c) __attribute__((format(printf0, 1,2))); // expected-error {{format attribute requires variadic function}}
  31. void e2(char *str, int c, ...) __attribute__((format(printf0, 2,3))); // expected-error {{format argument not a string type}}
  32. // FreeBSD usage
  33. #define __printf0like(fmt,va) __attribute__((__format__(__printf0__,fmt,va)))
  34. void null(int i, const char *a, ...) __printf0like(2,0); // no-error
  35. void null(int i, const char *a, ...) { // expected-note{{passing argument to parameter 'a' here}}
  36. if (a)
  37. (void)0/* vprintf(...) would go here */;
  38. }
  39. void callnull(void){
  40. null(0, 0); // no error
  41. null(0, (char*)0); // no error
  42. null(0, (void*)0); // no error
  43. null(0, (int*)0); // expected-warning {{incompatible pointer types}}
  44. }
  45. // FreeBSD kernel extensions
  46. void a3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1,2))); // no-error
  47. void b3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
  48. void c3(const char *a, ...) __attribute__((format(freebsd_kprintf, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
  49. void d3(const char *a, int c) __attribute__((format(freebsd_kprintf, 1,2))); // expected-error {{format attribute requires variadic function}}
  50. void e3(char *str, int c, ...) __attribute__((format(freebsd_kprintf, 2,3))); // expected-error {{format argument not a string type}}
  51. // PR4470
  52. int xx_vprintf(const char *, va_list);
  53. const char *foo(const char *format) __attribute__((format_arg(1)));
  54. void __attribute__((format(printf, 1, 0)))
  55. foo2(const char *fmt, va_list va) {
  56. xx_vprintf(foo(fmt), va);
  57. }
  58. // PR6542
  59. extern void gcc_format (const char *, ...)
  60. __attribute__ ((__format__(__gcc_diag__, 1, 2)));
  61. extern void gcc_cformat (const char *, ...)
  62. __attribute__ ((__format__(__gcc_cdiag__, 1, 2)));
  63. extern void gcc_cxxformat (const char *, ...)
  64. __attribute__ ((__format__(__gcc_cxxdiag__, 1, 2)));
  65. extern void gcc_tformat (const char *, ...)
  66. __attribute__ ((__format__(__gcc_tdiag__, 1, 2)));
  67. const char *foo3(const char *format) __attribute__((format_arg("foo"))); // expected-error{{'format_arg' attribute requires parameter 1 to be an integer constant}}