autotype.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifdef __CHECKER__
  2. #define is_type(X, T) _Static_assert([typeof(X)] == [T], "")
  3. #else
  4. #define is_type(X, T) _Static_assert(1, "")
  5. #endif
  6. struct s {
  7. int x;
  8. int bf:3;
  9. };
  10. extern char ch;
  11. extern const int ci;
  12. __auto_type i = 0; is_type(i, int);
  13. __auto_type m = 1UL; is_type(m, unsigned long);
  14. __auto_type l = (int)0L; is_type(l, int);
  15. __auto_type c = (char)'\n'; is_type(c, char);
  16. __auto_type p = &i; is_type(p, int *);
  17. __auto_type f = 0.0; is_type(f, double);
  18. __auto_type s = (struct s){0}; is_type(s, struct s);
  19. __auto_type pci = &ci; is_type(pci, const int *);
  20. // ~~: not valid for bitfield
  21. __auto_type b = (struct s){0}.bf; is_type(b, int);
  22. static __auto_type si = 0; is_type(si, int);
  23. const __auto_type ci = 0; is_type(ci, const int);
  24. __auto_type ch = (char) '\n'; is_type(ch, char);
  25. static int foo(int a)
  26. {
  27. __auto_type i = a; is_type(i, int);
  28. __auto_type c = ch; is_type(c, char);
  29. __auto_type ct = ci; is_type(&ct, const int *);
  30. return ct += i + c;
  31. }
  32. #define __as __attribute__((address_space(42)))
  33. extern int __as aa;
  34. __auto_type pa = &aa; is_type(pa, int __as *);
  35. /*
  36. * check-name: autotype
  37. * check-command: sparse -Wno-decl $file
  38. *
  39. * check-error-start
  40. autotype.c:25:13: warning: __auto_type on bitfield
  41. autotype.c:37:16: error: assignment to const expression
  42. * check-error-end
  43. */