2010-03-12 15:11:26 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s
|
|
|
|
|
|
|
|
// PR4806
|
|
|
|
namespace test0 {
|
|
|
|
class Box {
|
|
|
|
public:
|
|
|
|
int i;
|
|
|
|
volatile int j;
|
|
|
|
};
|
|
|
|
|
|
|
|
void doit() {
|
|
|
|
// pointer to volatile has side effect (thus no warning)
|
|
|
|
Box* box = new Box;
|
|
|
|
box->i; // expected-warning {{expression result unused}}
|
2012-05-24 08:47:05 +08:00
|
|
|
box->j; // expected-warning {{expression result unused}}
|
2010-03-12 15:11:26 +08:00
|
|
|
}
|
|
|
|
}
|
2011-09-20 02:51:20 +08:00
|
|
|
|
|
|
|
namespace test1 {
|
|
|
|
struct Foo {
|
|
|
|
int i;
|
|
|
|
bool operator==(const Foo& rhs) {
|
|
|
|
return i == rhs.i;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NOP(x) (x)
|
|
|
|
void b(Foo f1, Foo f2) {
|
|
|
|
NOP(f1 == f2); // expected-warning {{expression result unused}}
|
|
|
|
}
|
|
|
|
#undef NOP
|
|
|
|
}
|
2012-03-15 04:41:00 +08:00
|
|
|
|
|
|
|
namespace test2 {
|
2014-02-03 00:35:43 +08:00
|
|
|
extern "C++" {
|
2012-03-15 04:41:00 +08:00
|
|
|
namespace std {
|
|
|
|
template<typename T> struct basic_string {
|
|
|
|
struct X {};
|
|
|
|
void method() const {
|
|
|
|
X* x;
|
|
|
|
&x[0]; // expected-warning {{expression result unused}}
|
2014-12-18 05:57:17 +08:00
|
|
|
}
|
2012-03-15 04:41:00 +08:00
|
|
|
};
|
|
|
|
typedef basic_string<char> string;
|
2014-12-18 05:57:17 +08:00
|
|
|
void func(const std::string& str) {
|
2012-03-15 04:41:00 +08:00
|
|
|
str.method(); // expected-note {{in instantiation of member function}}
|
|
|
|
}
|
2014-12-18 05:57:17 +08:00
|
|
|
}
|
2012-03-15 04:41:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 21:15:58 +08:00
|
|
|
namespace test3 {
|
|
|
|
struct Used {
|
|
|
|
Used();
|
|
|
|
Used(int);
|
|
|
|
Used(int, int);
|
|
|
|
};
|
|
|
|
struct __attribute__((warn_unused)) Unused {
|
|
|
|
Unused();
|
|
|
|
Unused(int);
|
|
|
|
Unused(int, int);
|
|
|
|
};
|
|
|
|
void f() {
|
|
|
|
Used();
|
|
|
|
Used(1);
|
|
|
|
Used(1, 1);
|
|
|
|
Unused(); // expected-warning {{expression result unused}}
|
|
|
|
Unused(1); // expected-warning {{expression result unused}}
|
|
|
|
Unused(1, 1); // expected-warning {{expression result unused}}
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 05:57:17 +08:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
struct type_info {};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace test4 {
|
|
|
|
struct Good { Good &f(); };
|
|
|
|
struct Bad { virtual Bad& f(); };
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
int i = 0;
|
|
|
|
(void)typeid(++i); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
|
|
|
|
|
|
|
|
Good g;
|
|
|
|
(void)typeid(g.f()); // Ok; not a polymorphic use of a glvalue.
|
|
|
|
|
|
|
|
// This is a polymorphic use of a glvalue, which results in the typeid being
|
|
|
|
// evaluated instead of unevaluated.
|
|
|
|
Bad b;
|
|
|
|
(void)typeid(b.f()); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
|
|
|
|
|
2015-01-04 01:00:12 +08:00
|
|
|
// A dereference of a volatile pointer is a side effecting operation, however
|
|
|
|
// since it is idiomatic code, and the alternatives induce higher maintenance
|
|
|
|
// costs, it is allowed.
|
2014-12-18 05:57:17 +08:00
|
|
|
int * volatile x;
|
2015-01-04 01:00:12 +08:00
|
|
|
(void)sizeof(*x); // Ok
|
2014-12-18 05:57:17 +08:00
|
|
|
}
|
|
|
|
}
|