xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/misc-ps-cxx0x.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang --analyze -std=c++11 %s -Xclang -verify -o /dev/null
2*f4a2713aSLionel Sambuc 
test_static_assert()3*f4a2713aSLionel Sambuc void test_static_assert() {
4*f4a2713aSLionel Sambuc   static_assert(sizeof(void *) == sizeof(void*), "test_static_assert");
5*f4a2713aSLionel Sambuc }
6*f4a2713aSLionel Sambuc 
test_analyzer_working()7*f4a2713aSLionel Sambuc void test_analyzer_working() {
8*f4a2713aSLionel Sambuc   int *p = 0;
9*f4a2713aSLionel Sambuc   *p = 0xDEADBEEF; // expected-warning {{null}}
10*f4a2713aSLionel Sambuc }
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc // Test that pointer-to-member functions don't cause the analyzer
13*f4a2713aSLionel Sambuc // to crash.
14*f4a2713aSLionel Sambuc struct RDar10243398 {
15*f4a2713aSLionel Sambuc   void bar(int x);
16*f4a2713aSLionel Sambuc };
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc typedef void (RDar10243398::*RDar10243398MemberFn)(int x);
19*f4a2713aSLionel Sambuc 
test_rdar10243398(RDar10243398 * p)20*f4a2713aSLionel Sambuc void test_rdar10243398(RDar10243398 *p) {
21*f4a2713aSLionel Sambuc   RDar10243398MemberFn q = &RDar10243398::bar;
22*f4a2713aSLionel Sambuc   ((*p).*(q))(1);
23*f4a2713aSLionel Sambuc }
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc // Tests for CXXTemporaryObjectExpr.
26*f4a2713aSLionel Sambuc struct X {
27*f4a2713aSLionel Sambuc     X( int *ip, int );
28*f4a2713aSLionel Sambuc };
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc // Test to see if CXXTemporaryObjectExpr is being handled.
tempobj1()31*f4a2713aSLionel Sambuc int tempobj1()
32*f4a2713aSLionel Sambuc {
33*f4a2713aSLionel Sambuc   int j;
34*f4a2713aSLionel Sambuc   int i;
35*f4a2713aSLionel Sambuc   X a = X( &j, 1 );
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc   return i; // expected-warning {{Undefined or garbage value returned to caller}}
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // Test to see if CXXTemporaryObjectExpr invalidates arguments.
tempobj2()41*f4a2713aSLionel Sambuc int tempobj2()
42*f4a2713aSLionel Sambuc {
43*f4a2713aSLionel Sambuc   int j;
44*f4a2713aSLionel Sambuc   X a = X( &j, 1 );
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc   return j; // no-warning
47*f4a2713aSLionel Sambuc }
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc // Test for correct handling of C++ ForRange statement.
test1()51*f4a2713aSLionel Sambuc void test1() {
52*f4a2713aSLionel Sambuc   int array[2] = { 1, 2 };
53*f4a2713aSLionel Sambuc   int j = 0;
54*f4a2713aSLionel Sambuc   for ( int i : array )
55*f4a2713aSLionel Sambuc     j += i;
56*f4a2713aSLionel Sambuc   int *p = 0;
57*f4a2713aSLionel Sambuc   *p = 0xDEADBEEF;  // expected-warning {{null}}
58*f4a2713aSLionel Sambuc }
59*f4a2713aSLionel Sambuc 
test2()60*f4a2713aSLionel Sambuc void test2() {
61*f4a2713aSLionel Sambuc   int array[2] = { 1, 2 };
62*f4a2713aSLionel Sambuc   int j = 0;
63*f4a2713aSLionel Sambuc   for (int i : array)
64*f4a2713aSLionel Sambuc     j += i;
65*f4a2713aSLionel Sambuc   if (j == 3)
66*f4a2713aSLionel Sambuc     return;
67*f4a2713aSLionel Sambuc   int *p = 0;
68*f4a2713aSLionel Sambuc   *p = 0xDEADBEEF;  // no-warning
69*f4a2713aSLionel Sambuc }
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc // Do not crash on the following when constructing the
72*f4a2713aSLionel Sambuc // callgraph.
73*f4a2713aSLionel Sambuc struct RDar11178609 {
74*f4a2713aSLionel Sambuc   ~RDar11178609() = delete;
75*f4a2713aSLionel Sambuc };
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // Tests that dynamic_cast handles references to C++ classes.  Previously
78*f4a2713aSLionel Sambuc // this crashed.
79*f4a2713aSLionel Sambuc class rdar11817693_BaseBase {};
80*f4a2713aSLionel Sambuc class rdar11817693_BaseInterface {};
81*f4a2713aSLionel Sambuc class rdar11817693_Base : public rdar11817693_BaseBase, public rdar11817693_BaseInterface {};
82*f4a2713aSLionel Sambuc class rdar11817693 : public rdar11817693_Base {
83*f4a2713aSLionel Sambuc   virtual void operator=(const rdar11817693_BaseBase& src);
84*f4a2713aSLionel Sambuc   void operator=(const rdar11817693& src);
85*f4a2713aSLionel Sambuc };
operator =(const rdar11817693 & src)86*f4a2713aSLionel Sambuc void rdar11817693::operator=(const rdar11817693& src) {
87*f4a2713aSLionel Sambuc   operator=(dynamic_cast<const rdar11817693_BaseBase&>(src));
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc 
90