[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
Wed Oct 30 07:10:13 PDT 2019


gamesh411 updated this revision to Diff 227090.
gamesh411 added a comment.

- Add capture by reference test
- Add another class to ensure reachability inside function call is independent from each other in the first 2 test cases


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69602/new/

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,93 @@
+// 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_from_simple_copy() {
+  clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
+};
+
+void reached_function_from_lambda_capture() {
+  clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
+};
+
+struct incr_on_copy_for_simple_usage {
+  int &i;
+  incr_on_copy_for_simple_usage(int &i) : i(i) {}
+  incr_on_copy_for_simple_usage(const incr_on_copy_for_simple_usage &o)
+      : i(++o.i) {
+    reached_function_from_simple_copy();
+  }
+  incr_on_copy_for_simple_usage &
+  operator=(const incr_on_copy_for_simple_usage &o) = delete;
+  incr_on_copy_for_simple_usage &
+  operator=(incr_on_copy_for_simple_usage &&o) = delete;
+  ~incr_on_copy_for_simple_usage() = default;
+};
+
+struct incr_on_copy_for_lambda_usage {
+  int &i;
+  incr_on_copy_for_lambda_usage(int &i) : i(i) {}
+  incr_on_copy_for_lambda_usage(const incr_on_copy_for_lambda_usage &o)
+      : i(++o.i) {
+    reached_function_from_lambda_capture();
+  }
+  incr_on_copy_for_lambda_usage &
+  operator=(const incr_on_copy_for_lambda_usage &o) = delete;
+  incr_on_copy_for_lambda_usage &
+  operator=(incr_on_copy_for_lambda_usage &&o) = delete;
+  ~incr_on_copy_for_lambda_usage() = default;
+};
+
+void test_simple_copy() {
+  int a = 0;
+
+  incr_on_copy_for_simple_usage ioc(a);
+
+  clang_analyzer_eval(ioc.i == 0); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 0);     // expected-warning{{TRUE}}
+
+  // Explicit copy constructor call.
+  incr_on_copy_for_simple_usage ioc2(ioc);
+
+  clang_analyzer_eval(ioc2.i == 1); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 1);      // expected-warning{{TRUE}}
+}
+
+void test_lambda_capture_by_value() {
+  int a = 0;
+
+  incr_on_copy_for_lambda_usage 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}}
+}
+
+void test_lambda_capture_by_ref() {
+  int a = 0;
+
+  incr_on_copy_for_lambda_usage ioc(a);
+
+  clang_analyzer_eval(ioc.i == 0); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 0);     // expected-warning{{TRUE}}
+
+  // Do not call copy constructor in case of capture-by-reference.
+  [&ioc]() {
+    clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
+  }();
+
+  clang_analyzer_eval(ioc.i == 0); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == 0);     // expected-warning{{TRUE}}
+};


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


More information about the cfe-commits mailing list