unused-expr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code
  2. int foo(int X, int Y);
  3. double sqrt(double X); // implicitly const because of no -fmath-errno!
  4. void bar(volatile int *VP, int *P, int A,
  5. _Complex double C, volatile _Complex double VC) {
  6. VP < P; // expected-warning {{relational comparison result unused}}
  7. (void)A;
  8. (void)foo(1,2); // no warning.
  9. A < foo(1, 2); // expected-warning {{relational comparison result unused}}
  10. foo(1,2)+foo(4,3); // expected-warning {{expression result unused}}
  11. *P; // expected-warning {{expression result unused}}
  12. *VP; // no warning.
  13. P[4]; // expected-warning {{expression result unused}}
  14. VP[4]; // no warning.
  15. __real__ C; // expected-warning {{expression result unused}}
  16. __real__ VC;
  17. // We know this can't change errno because of no -fmath-errno.
  18. sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}}
  19. }
  20. extern void t1();
  21. extern void t2();
  22. void t3(int c) {
  23. c ? t1() : t2();
  24. }
  25. // This shouldn't warn: the expr at the end of the stmtexpr really is used.
  26. int stmt_expr(int x, int y) {
  27. return ({int _a = x, _b = y; _a > _b ? _a : _b; });
  28. }
  29. void nowarn(unsigned char* a, unsigned char* b)
  30. {
  31. unsigned char c = 1;
  32. *a |= c, *b += c;
  33. // PR4633
  34. int y, x;
  35. ((void)0), y = x;
  36. }
  37. void t4(int a) {
  38. int b = 0;
  39. if (a)
  40. b < 1; // expected-warning{{relational comparison result unused}}
  41. else
  42. b < 2; // expected-warning{{relational comparison result unused}}
  43. while (1)
  44. b < 3; // expected-warning{{relational comparison result unused}}
  45. do
  46. b < 4; // expected-warning{{relational comparison result unused}}
  47. while (1);
  48. for (;;)
  49. b < 5; // expected-warning{{relational comparison result unused}}
  50. for (b < 1;;) {} // expected-warning{{relational comparison result unused}}
  51. for (;b < 1;) {}
  52. for (;;b < 1) {} // expected-warning{{relational comparison result unused}}
  53. }
  54. // rdar://7186119
  55. int t5f(void) __attribute__((warn_unused_result));
  56. void t5() {
  57. t5f(); // expected-warning {{ignoring return value of function declared with warn_unused_result}}
  58. }
  59. int fn1() __attribute__ ((warn_unused_result));
  60. int fn2() __attribute__ ((pure));
  61. int fn3() __attribute__ ((__const));
  62. // rdar://6587766
  63. int t6() {
  64. if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings
  65. return -1;
  66. fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
  67. fn2(92, 21); // expected-warning {{ignoring return value of function declared with pure attribute}}
  68. fn3(42); // expected-warning {{ignoring return value of function declared with const attribute}}
  69. __builtin_abs(0); // expected-warning {{ignoring return value of function declared with const attribute}}
  70. (void)0, fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
  71. return 0;
  72. }
  73. int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}}
  74. // PR4010
  75. int (*fn4)(void) __attribute__ ((warn_unused_result));
  76. void t8() {
  77. fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
  78. }
  79. void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}}
  80. // rdar://7410924
  81. void *some_function(void);
  82. void t10() {
  83. (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}
  84. }
  85. void f(int i, ...) {
  86. __builtin_va_list ap;
  87. __builtin_va_start(ap, i);
  88. __builtin_va_arg(ap, int);
  89. __builtin_va_end(ap);
  90. }
  91. // PR8371
  92. int fn5() __attribute__ ((__const));
  93. // Don't warn for unused expressions in macro bodies; however, do warn for
  94. // unused expressions in macro arguments. Macros below are reduced from code
  95. // found in the wild.
  96. #define NOP(a) (a)
  97. #define M1(a, b) (long)foo((a), (b))
  98. #define M2 (long)0;
  99. #define M3(a) (t3(a), fn2())
  100. #define M4(a, b) (foo((a), (b)) ? 0 : t3(a), 1)
  101. #define M5(a, b) (foo((a), (b)), 1)
  102. #define M6() fn1()
  103. #define M7() fn2()
  104. void t11(int i, int j) {
  105. M1(i, j); // no warning
  106. NOP((long)foo(i, j)); // expected-warning {{expression result unused}}
  107. M2; // no warning
  108. NOP((long)0); // expected-warning {{expression result unused}}
  109. M3(i); // no warning
  110. NOP((t3(i), fn2())); // expected-warning {{ignoring return value}}
  111. M4(i, j); // no warning
  112. NOP((foo(i, j) ? 0 : t3(i), 1)); // expected-warning {{expression result unused}}
  113. M5(i, j); // no warning
  114. NOP((foo(i, j), 1)); // expected-warning {{expression result unused}}
  115. M6(); // expected-warning {{ignoring return value}}
  116. M7(); // no warning
  117. }
  118. #undef NOP
  119. #undef M1
  120. #undef M2
  121. #undef M3
  122. #undef M4
  123. #undef M5
  124. #undef M6
  125. #undef M7