173d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
273d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
373d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregorvoid odr_used() {
473d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  int i = 17;
573d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  [i]{}();
673d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor}
773d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
873d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregorstruct ReachingThis {
973d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  static void static_foo() {
1073d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    (void)[this](){}; // expected-error{{'this' cannot be captured in this context}}
1173d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
1273d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    struct Local {
1373d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor      int i;
1473d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
1573d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor      void bar() {
1673d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor        (void)[this](){};
1773d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor        (void)[&](){i = 7; };
1873d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor      }
1973d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    };
2073d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  }
2173d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
2273d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  void foo() {
2373d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    (void)[this](){};
2473d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
2573d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    struct Local {
2673d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor      int i;
2773d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
2873d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor      static void static_bar() {
2973d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor        (void)[this](){}; // expected-error{{'this' cannot be captured in this context}}
30a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith        (void)[&](){i = 7; }; // expected-error{{invalid use of member 'i' in static member function}}
3173d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor      }
3273d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    };
3373d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  }
3473d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor};
3573d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
3673d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregorvoid immediately_enclosing(int i) { // expected-note{{'i' declared here}}
3773d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  [i]() {
3873d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    [i] {}();
3973d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  }();
4073d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
4173d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  [=]() {
4273d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    [i] {}();
4373d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  }();
4473d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor
4573d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  []() { // expected-note{{lambda expression begins here}}
4673d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor    [i] {}(); // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}
4773d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor  }();
4873d90928c0462daf0665fd7f8e44ca00d896540dDouglas Gregor}
4967b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor
5067b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregorvoid f1(int i) { // expected-note{{declared here}}
5167b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor  int const N = 20;
5267b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor  auto m1 = [=]{
5367b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    int const M = 30;
5467b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    auto m2 = [i]{
55ac6260187b6b2f26faa9264d170d649a501f58a9Eli Friedman      int x[N][M];
56ac6260187b6b2f26faa9264d170d649a501f58a9Eli Friedman      x[0][0] = i;
5767b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    };
5867b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    (void)N;
5967b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    (void)M;
6067b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    (void)m2;
6167b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor  };
6267b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor  struct s1 {
6367b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    int f;
6467b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    void work(int n) { // expected-note{{declared here}}
6567b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor      int m = n*n;
6667b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor      int j = 40; // expected-note{{declared here}}
67f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      auto m3 = [this,m] { // expected-note 3{{lambda expression begins here}}
6867b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor        auto m4 = [&,j] { // expected-error{{variable 'j' cannot be implicitly captured in a lambda with no capture-default specified}}
6967b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor          int x = n; // expected-error{{variable 'n' cannot be implicitly captured in a lambda with no capture-default specified}}
7067b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor          x += m;
71f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor          x += i; // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}
7267b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor          x += f;
7367b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor        };
7467b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor      };
7567b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor    }
7667b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor  };
7767b2c554dc12f589471713b7b01e9c94257ae593Douglas Gregor}
78