• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -analyze -analyzer-checker=core,deadcode,debug.ExprInspection -verify %s
2 
3 int clang_analyzer_eval(int);
4 
generalizedCapture()5 void generalizedCapture() {
6   int v = 7;
7   auto lambda = [x=v]() {
8     return x;
9   };
10 
11   int result = lambda();
12   clang_analyzer_eval(result == 7); // expected-warning {{TRUE}}
13 }
14 
sideEffectsInGeneralizedCapture()15 void sideEffectsInGeneralizedCapture() {
16   int v = 7;
17   auto lambda = [x=v++]() {
18     return x;
19   };
20   clang_analyzer_eval(v == 8); // expected-warning {{TRUE}}
21 
22   int r1 = lambda();
23   int r2 = lambda();
24   clang_analyzer_eval(r1 == 7); // expected-warning {{TRUE}}
25   clang_analyzer_eval(r2 == 7); // expected-warning {{TRUE}}
26   clang_analyzer_eval(v == 8); // expected-warning {{TRUE}}
27 }
28 
addOne(int p)29 int addOne(int p) {
30  return p + 1;
31 }
32 
inliningInGeneralizedCapture()33 void inliningInGeneralizedCapture() {
34   int v = 7;
35   auto lambda = [x=addOne(v)]() {
36     return x;
37   };
38 
39   int result = lambda();
40   clang_analyzer_eval(result == 8); // expected-warning {{TRUE}}
41 }
42 
caseSplitInGeneralizedCapture(bool p)43 void caseSplitInGeneralizedCapture(bool p) {
44   auto lambda = [x=(p ? 1 : 2)]() {
45     return x;
46   };
47 
48   int result = lambda();
49   clang_analyzer_eval(result == 1); // expected-warning {{FALSE}} expected-warning {{TRUE}}
50 }
51