enable_if.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // RUN: %clang_cc1 %s -verify
  2. // RUN: %clang_cc1 %s -DCODEGEN -emit-llvm -o - | FileCheck %s
  3. #define O_CREAT 0x100
  4. typedef int mode_t;
  5. typedef unsigned long size_t;
  6. int open(const char *pathname, int flags) __attribute__((enable_if(!(flags & O_CREAT), "must specify mode when using O_CREAT"))) __attribute__((overloadable)); // expected-note{{candidate disabled: must specify mode when using O_CREAT}}
  7. int open(const char *pathname, int flags, mode_t mode) __attribute__((overloadable)); // expected-note{{candidate function not viable: requires 3 arguments, but 2 were provided}}
  8. void test1() {
  9. #ifndef CODEGEN
  10. open("path", O_CREAT); // expected-error{{no matching function for call to 'open'}}
  11. #endif
  12. open("path", O_CREAT, 0660);
  13. open("path", 0);
  14. open("path", 0, 0);
  15. }
  16. size_t __strnlen_chk(const char *s, size_t requested_amount, size_t s_len);
  17. size_t strnlen(const char *s, size_t maxlen) // expected-note{{candidate function}}
  18. __attribute__((overloadable))
  19. __asm__("strnlen_real1");
  20. __attribute__((always_inline))
  21. inline size_t strnlen(const char *s, size_t maxlen) // expected-note{{candidate function}}
  22. __attribute__((overloadable))
  23. __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
  24. "chosen when target buffer size is known")))
  25. {
  26. return __strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
  27. }
  28. size_t strnlen(const char *s, size_t maxlen) // expected-note{{candidate disabled: chosen when 'maxlen' is known to be less than or equal to the buffer size}}
  29. __attribute__((overloadable))
  30. __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
  31. "chosen when target buffer size is known")))
  32. __attribute__((enable_if(maxlen <= __builtin_object_size(s, 0),
  33. "chosen when 'maxlen' is known to be less than or equal to the buffer size")))
  34. __asm__("strnlen_real2");
  35. size_t strnlen(const char *s, size_t maxlen) // expected-note{{candidate function has been explicitly made unavailable}}
  36. __attribute__((overloadable))
  37. __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
  38. "chosen when target buffer size is known")))
  39. __attribute__((enable_if(maxlen > __builtin_object_size(s, 0),
  40. "chosen when 'maxlen' is larger than the buffer size")))
  41. __attribute__((unavailable("'maxlen' is larger than the buffer size")));
  42. void test2(const char *s, int i) {
  43. // CHECK: define {{.*}}void @test2
  44. const char c[123];
  45. strnlen(s, i);
  46. // CHECK: call {{.*}}strnlen_real1
  47. strnlen(s, 999);
  48. // CHECK: call {{.*}}strnlen_real1
  49. strnlen(c, 1);
  50. // CHECK: call {{.*}}strnlen_real2
  51. strnlen(c, i);
  52. // CHECK: call {{.*}}strnlen_chk
  53. #ifndef CODEGEN
  54. strnlen(c, 999); // expected-error{{call to unavailable function 'strnlen': 'maxlen' is larger than the buffer size}}
  55. #endif
  56. }
  57. int isdigit(int c) __attribute__((overloadable)); // expected-note{{candidate function}}
  58. int isdigit(int c) __attribute__((overloadable)) // expected-note{{candidate function has been explicitly made unavailable}}
  59. __attribute__((enable_if(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
  60. __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
  61. void test3(int c) {
  62. isdigit(c);
  63. isdigit(10);
  64. #ifndef CODEGEN
  65. isdigit(-10); // expected-error{{call to unavailable function 'isdigit': 'c' must have the value of an unsigned char or EOF}}
  66. #endif
  67. }
  68. // Verify that the alternate spelling __enable_if__ works as well.
  69. int isdigit2(int c) __attribute__((overloadable)); // expected-note{{candidate function}}
  70. int isdigit2(int c) __attribute__((overloadable)) // expected-note{{candidate function has been explicitly made unavailable}}
  71. __attribute__((__enable_if__(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
  72. __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
  73. void test4(int c) {
  74. isdigit2(c);
  75. isdigit2(10);
  76. #ifndef CODEGEN
  77. isdigit2(-10); // expected-error{{call to unavailable function 'isdigit2': 'c' must have the value of an unsigned char or EOF}}
  78. #endif
  79. }
  80. #ifndef CODEGEN
  81. __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) void f1(int n); // expected-error{{use of undeclared identifier 'n'}}
  82. int n __attribute__((enable_if(1, "always chosen"))); // expected-warning{{'enable_if' attribute only applies to functions}}
  83. void f(int n) __attribute__((enable_if("chosen when 'n' is zero", n == 0))); // expected-error{{'enable_if' attribute requires a string}}
  84. void f(int n) __attribute__((enable_if())); // expected-error{{'enable_if' attribute requires exactly 2 arguments}}
  85. void f(int n) __attribute__((enable_if(unresolvedid, "chosen when 'unresolvedid' is non-zero"))); // expected-error{{use of undeclared identifier 'unresolvedid'}}
  86. int global;
  87. void f(int n) __attribute__((enable_if(global == 0, "chosen when 'global' is zero"))); // expected-error{{'enable_if' attribute expression never produces a constant expression}} // expected-note{{subexpression not valid in a constant expression}}
  88. const int cst = 7;
  89. void return_cst(void) __attribute__((overloadable)) __attribute__((enable_if(cst == 7, "chosen when 'cst' is 7")));
  90. void test_return_cst() { return_cst(); }
  91. #endif