================
@@ -1951,5 +1970,95 @@ TEST_F(LifetimeAnalysisTest, 
LambdaInitCaptureViewByValue) {
   )");
   EXPECT_THAT(Origin("lambda"), HasLoansTo({"obj"}, "after_lambda"));
 }
+
+// ========================================================================= //
+//                    Tests for buildOriginFlowChain
+// ========================================================================= //
+
+TEST_F(LifetimeAnalysisTest, BuildOriginFlowChainWithErrorTargetLoan) {
+  SetupTest(R"(
+    void target() {
+      int tgt = 2;
+      int *a = &tgt;
+      int *s = a;
+      POINT(after_use);
+    }
+  )");
+
+#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
+  EXPECT_DEATH(Helper->buildOriginFlowChainInOneBlock("s", "a", "after_use"),
+               "TargetLoan must be present in the StartOID at the StartPoint");
+#endif
+}
+
+TEST_F(LifetimeAnalysisTest, BuildOriginFlowChainWithSelfAssignment) {
+  SetupTest(R"(
+    void target() {
+      int tgt = 2;
+      int *a = &tgt;
+      int *b = a;
+      a = b;
+      a = a;
+      int *s = a;
+      POINT(after_use);
+    }
+  )");
+
+  const llvm::SmallVector<OriginID> OriginFlowChain =
+      Helper->buildOriginFlowChainInOneBlock("s", "tgt", "after_use");
+
+  EXPECT_TRUE(
+      llvm::is_contained(OriginFlowChain, *Helper->getOriginForDecl("a")));
+}
+
+TEST_F(LifetimeAnalysisTest, BuildOriginFlowChainWithLifetimeBound) {
+  SetupTest(R"(
+    int* choose(int* a [[clang::lifetimebound]], int* b  
[[clang::lifetimebound]]);
+    void target() {
+      int tgta = 1, tgtb = 2;
+      int *a = &tgta;
+      int *b = &tgtb;
+      int *result = choose(a, b);
+      result = choose(result , result);
+      int *s = result;
+      POINT(after_use);
+    }
+  )");
+
+  const llvm::SmallVector<OriginID> OriginFlowChainForTgtA =
+      Helper->buildOriginFlowChainInOneBlock("s", "tgta", "after_use");
+
+  const llvm::SmallVector<OriginID> OriginFlowChainForTgtB =
+      Helper->buildOriginFlowChainInOneBlock("s", "tgtb", "after_use");
+
+  EXPECT_TRUE(llvm::is_contained(OriginFlowChainForTgtA,
+                                 *Helper->getOriginForDecl("a")));
+  EXPECT_FALSE(llvm::is_contained(OriginFlowChainForTgtA,
+                                  *Helper->getOriginForDecl("b")));
+  EXPECT_TRUE(llvm::is_contained(OriginFlowChainForTgtB,
+                                 *Helper->getOriginForDecl("b")));
+  EXPECT_FALSE(llvm::is_contained(OriginFlowChainForTgtB,
+                                  *Helper->getOriginForDecl("a")));
----------------
usx95 wrote:

gtest matchers are idiomatic for things like these.

```cpp
EXPECT_THAT(OriginFlowChainForTgtA, 
            Contains(*Helper->getOriginForDecl("a")));
EXPECT_THAT(OriginFlowChainForTgtA, 
            Not(Contains(*Helper->getOriginForDecl("b"))));
EXPECT_THAT(OriginFlowChainForTgtB, 
            Contains(*Helper->getOriginForDecl("b")));
EXPECT_THAT(OriginFlowChainForTgtB, 
            Not(Contains(*Helper->getOriginForDecl("a"))));
```
Similarly for all the tests.

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

Reply via email to