1a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith// RUN: %clang_cc1 -fsyntax-only -Wdangling-field -verify -std=c++11 %s
2bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth
3bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthstruct X {
4bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  X(int);
5bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth};
6bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthstruct Y {
7bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  operator X*();
8bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  operator X&();
9bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth};
10bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth
11bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthstruct S {
12bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  int &x, *y;  // expected-note {{reference member declared here}} \
13bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth               // expected-note {{pointer member declared here}}
14bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  S(int i)
15bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth    : x(i),    // expected-warning {{binding reference member 'x' to stack allocated parameter 'i'}}
16bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth      y(&i) {} // expected-warning {{initializing pointer member 'y' with the stack address of parameter 'i'}}
17bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  S(int &i) : x(i), y(&i) {} // no-warning: reference parameter
18bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  S(int *i) : x(*i), y(i) {} // no-warning: pointer parameter
19bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth};
20bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth
21bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthstruct S2 {
22bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  const X &x; // expected-note {{reference member declared here}}
23bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  S2(int i) : x(i) {} // expected-warning {{binding reference member 'x' to a temporary}}
24bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth};
25bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth
26bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthstruct S3 {
27bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  X &x1, *x2;
28bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  S3(Y y) : x1(y), x2(y) {} // no-warning: conversion operator
29bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth};
30bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth
31bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthtemplate <typename T> struct S4 {
32bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  T x; // expected-note {{reference member declared here}}
33bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth  S4(int i) : x(i) {} // expected-warning {{binding reference member 'x' to stack allocated parameter 'i'}}
34bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth};
35bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruth
36bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthtemplate struct S4<int>; // no warning from this instantiation
37bf3380a317ab09ae7f23c1bc17cb562f8175a986Chandler Carruthtemplate struct S4<int&>; // expected-note {{in instantiation}}
38a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith
39a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smithstruct S5 {
40a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith  const X &x; // expected-note {{here}}
41a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith};
42a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard SmithS5 s5 = { 0 }; // ok, lifetime-extended
43a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith
44a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smithstruct S6 {
45a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith  S5 s5; // expected-note {{here}}
46a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith  S6() : s5 { 0 } {} // expected-warning {{binding reference subobject of member 's5' to a temporary}}
47a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith};
48a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith
49a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smithstruct S7 : S5 {
50a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith  S7() : S5 { 0 } {} // expected-warning {{binding reference member 'x' to a temporary}}
51a4bb99cd0055ba0e1f3107890e5b6cbe31e6d1ccRichard Smith};
52