================
@@ -0,0 +1,81 @@
+// RUN: %clang_analyze_cc1 
-analyzer-checker=core,alpha.cplusplus.ReportDanglingPtrDeref \
+// RUN:   -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
+
+void test_case_one() {
+  int *ptr = nullptr;
+  {
+    int num = 5;
+    ptr = #
+  }
+  *ptr = 6;
+  // expected-warning@-1 {{Use of 'num' after its lifetime ended}}
+  // expected-note@-2 {{Use of 'num' after its lifetime ended}}
+}
+
+void test_case_two() {
+  int *ptr_one = nullptr;
+  int *ptr_two = nullptr;
+  {
+    int n = 1;
+    int m = 2;
+    ptr_one = &n;
+    ptr_two = &m;
+  }
+  *ptr_one = 6;
+  *ptr_two = 7; 
+  // expected-warning@-2 {{Use of 'n' after its lifetime ended}}
+  // expected-warning@-2 {{Use of 'm' after its lifetime ended}}
+  // expected-note@-4 {{Use of 'n' after its lifetime ended}}
+  // expected-note@-4 {{Use of 'm' after its lifetime ended}}
+}
+
+void escape(int *ptr);
+
+void test_case_three() {
+  int num = 5;
+  int *ptr = #
+  {
+    *ptr = 6; // no-warning
+  }
+}
+
+void test_case_four() {
+  int *ptr = nullptr;
+  {
+    int num = 5;
+    ptr = #
+  }
+  int i = *ptr;
+  // expected-warning@-1 {{Use of 'num' after its lifetime ended}}
+  // expected-note@-2 {{Use of 'num' after its lifetime ended}}
+  i += i;
+}
+
+void test_case_five() {
+  int *ptr = nullptr;
+  for(int i = 0; i < 10; ++i) {
+    ptr = &i;
+  }
+  escape(ptr); // no-warning
+}
+
+void test_case_six() {
+  for(int i = 0; i < 10; ++i) {
+    int *ptr = &i;
+    escape(ptr); // no-warning
+  }
+}
+
+void test_case_seven() {
+  int *ptr = nullptr;
+  for (int i = 0; i < 10; ++i) {
+    ptr = &i;
+    escape(ptr);
+  }
+  *ptr = 6;
+  // expected-warning@-1 {{Use of 'i' after its lifetime ended}}
+  // expected-note@-2 {{Use of 'i' after its lifetime ended}}
+  // expected-note@-7 {{Loop condition is true.  Entering loop body}}
+  // expected-note@-8 {{Assuming 'i' is >= 10}}
+  // expected-note@-9 {{Loop condition is false. Execution continues on line 
71}}
----------------
steakhal wrote:

You should probably move these closer to the pointed line. You can also target 
them from above with `@+1`.

https://github.com/llvm/llvm-project/pull/209278
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to