xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-dangling-field.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wdangling-field -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct X {
4*f4a2713aSLionel Sambuc   X(int);
5*f4a2713aSLionel Sambuc };
6*f4a2713aSLionel Sambuc struct Y {
7*f4a2713aSLionel Sambuc   operator X*();
8*f4a2713aSLionel Sambuc   operator X&();
9*f4a2713aSLionel Sambuc };
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc struct S {
12*f4a2713aSLionel Sambuc   int &x, *y;  // expected-note {{reference member declared here}} \
13*f4a2713aSLionel Sambuc                // expected-note {{pointer member declared here}}
SS14*f4a2713aSLionel Sambuc   S(int i)
15*f4a2713aSLionel Sambuc     : x(i),    // expected-warning {{binding reference member 'x' to stack allocated parameter 'i'}}
16*f4a2713aSLionel Sambuc       y(&i) {} // expected-warning {{initializing pointer member 'y' with the stack address of parameter 'i'}}
SS17*f4a2713aSLionel Sambuc   S(int &i) : x(i), y(&i) {} // no-warning: reference parameter
SS18*f4a2713aSLionel Sambuc   S(int *i) : x(*i), y(i) {} // no-warning: pointer parameter
19*f4a2713aSLionel Sambuc };
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc struct S2 {
22*f4a2713aSLionel Sambuc   const X &x; // expected-note {{reference member declared here}}
S2S223*f4a2713aSLionel Sambuc   S2(int i) : x(i) {} // expected-warning {{binding reference member 'x' to a temporary}}
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc struct S3 {
27*f4a2713aSLionel Sambuc   X &x1, *x2;
S3S328*f4a2713aSLionel Sambuc   S3(Y y) : x1(y), x2(y) {} // no-warning: conversion operator
29*f4a2713aSLionel Sambuc };
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc template <typename T> struct S4 {
32*f4a2713aSLionel Sambuc   T x; // expected-note {{reference member declared here}}
S4S433*f4a2713aSLionel Sambuc   S4(int i) : x(i) {} // expected-warning {{binding reference member 'x' to stack allocated parameter 'i'}}
34*f4a2713aSLionel Sambuc };
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc template struct S4<int>; // no warning from this instantiation
37*f4a2713aSLionel Sambuc template struct S4<int&>; // expected-note {{in instantiation}}
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc struct S5 {
40*f4a2713aSLionel Sambuc   const X &x; // expected-note {{here}}
41*f4a2713aSLionel Sambuc };
42*f4a2713aSLionel Sambuc S5 s5 = { 0 }; // ok, lifetime-extended
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc struct S6 {
45*f4a2713aSLionel Sambuc   S5 s5; // expected-note {{here}}
S6S646*f4a2713aSLionel Sambuc   S6() : s5 { 0 } {} // expected-warning {{binding reference subobject of member 's5' to a temporary}}
47*f4a2713aSLionel Sambuc };
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc struct S7 : S5 {
S7S750*f4a2713aSLionel Sambuc   S7() : S5 { 0 } {} // expected-warning {{binding reference member 'x' to a temporary}}
51*f4a2713aSLionel Sambuc };
52