r254114 - [analyzer] Add tests for generalized lambda capture (C++14). NFC.

Devin Coughlin via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 16:11:23 PST 2015


Author: dcoughlin
Date: Wed Nov 25 18:11:23 2015
New Revision: 254114

URL: http://llvm.org/viewvc/llvm-project?rev=254114&view=rev
Log:
[analyzer] Add tests for generalized lambda capture (C++14). NFC.

Add tests demonstrating that the analyzer supports generalized lambda capture. This
support falls out naturally from the work Gábor Horváth did adding C++11 lambdas to
the analyzer.

Added:
    cfe/trunk/test/Analysis/lambdas-generalized-capture.cpp

Added: cfe/trunk/test/Analysis/lambdas-generalized-capture.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/lambdas-generalized-capture.cpp?rev=254114&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/lambdas-generalized-capture.cpp (added)
+++ cfe/trunk/test/Analysis/lambdas-generalized-capture.cpp Wed Nov 25 18:11:23 2015
@@ -0,0 +1,50 @@
+// RUN: %clang_cc1 -std=c++14 -fsyntax-only -analyze -analyzer-checker=core,deadcode,debug.ExprInspection -verify %s
+
+int clang_analyzer_eval(int);
+
+void generalizedCapture() {
+  int v = 7;
+  auto lambda = [x=v]() {
+    return x;
+  };
+
+  int result = lambda();
+  clang_analyzer_eval(result == 7); // expected-warning {{TRUE}}
+}
+
+void sideEffectsInGeneralizedCapture() {
+  int v = 7;
+  auto lambda = [x=v++]() {
+    return x;
+  };
+  clang_analyzer_eval(v == 8); // expected-warning {{TRUE}}
+
+  int r1 = lambda();
+  int r2 = lambda();
+  clang_analyzer_eval(r1 == 7); // expected-warning {{TRUE}}
+  clang_analyzer_eval(r2 == 7); // expected-warning {{TRUE}}
+  clang_analyzer_eval(v == 8); // expected-warning {{TRUE}}
+}
+
+int addOne(int p) {
+ return p + 1;
+}
+
+void inliningInGeneralizedCapture() {
+  int v = 7;
+  auto lambda = [x=addOne(v)]() {
+    return x;
+  };
+
+  int result = lambda();
+  clang_analyzer_eval(result == 8); // expected-warning {{TRUE}}
+}
+
+void caseSplitInGeneralizedCapture(bool p) {
+  auto lambda = [x=(p ? 1 : 2)]() {
+    return x;
+  };
+
+  int result = lambda();
+  clang_analyzer_eval(result == 1); // expected-warning {{FALSE}} expected-warning {{TRUE}}
+}




More information about the cfe-commits mailing list