[PATCH] D69602: [analyzer] Test case for lambda capture by value modelling

Endre Fülöp via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 29 20:02:11 PDT 2019


gamesh411 created this revision.
gamesh411 added reviewers: NoQ, Szelethus, baloghadamsoftware.
Herald added subscribers: cfe-commits, Charusso, dkrupp, donat.nagy, mikhail.ramalho, a.sidorin, rnkovacs, szepet, xazax.hun, whisperity.
Herald added a project: clang.

This test provides test coverage for the following feature:
Variables which are captured by value into a lambda require a call to a
copy constructor.
This is an item on the open projects list:
https://clang-analyzer.llvm.org/open_projects.html


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69602

Files:
  clang/test/Analysis/clangsa_unsupported_features/handle_constructors_for_lambda_captures.cpp


Index: clang/test/Analysis/clangsa_unsupported_features/handle_constructors_for_lambda_captures.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/clangsa_unsupported_features/handle_constructors_for_lambda_captures.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -fsyntax-only -analyze \
+// RUN:   -analyzer-checker=core,debug.ExprInspection %s -verify
+
+// Handle constructors for lambda captures
+// Variables which are captured by value into a lambda require a call to a copy
+// constructor.
+void clang_analyzer_eval(bool);
+void clang_analyzer_warnIfReached();
+
+void reached_function() {
+  clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
+};
+
+struct incr_on_copy {
+  int &i;
+  incr_on_copy(int &i) : i(i) {}
+  incr_on_copy(const incr_on_copy &o) : i(++o.i) { reached_function(); }
+  incr_on_copy &operator=(const incr_on_copy &o) = delete;
+  incr_on_copy &operator=(incr_on_copy &&o) = delete;
+  ~incr_on_copy() = default;
+};
+
+void test_simple_copy() {
+  int a = 0;
+
+  incr_on_copy ioc1(a);
+
+  clang_analyzer_eval(ioc1.i == 0); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 0);      // expected-warning{{TRUE}}
+
+  // Explicit copy constructor call.
+  incr_on_copy ioc2(ioc1);
+
+  clang_analyzer_eval(ioc2.i == 1); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 1);      // expected-warning{{TRUE}}
+}
+
+void test_lambda_capture() {
+  int a = 0;
+
+  incr_on_copy ioc(a);
+
+  clang_analyzer_eval(ioc.i == 0); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 0);     // expected-warning{{TRUE}}
+
+  // Implicitly call copy constructor in case of capture-by-value.
+  [ioc]() {
+    clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
+  }();
+
+  clang_analyzer_eval(ioc.i == 1); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 1);     // expected-warning{{TRUE}}
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69602.227021.patch
Type: text/x-patch
Size: 1917 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191030/b8f9d90f/attachment.bin>


More information about the cfe-commits mailing list