[clang] [clang][dataflow] Model assignment to derived class from base. (PR #85064)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 18 08:01:06 PDT 2024


================
@@ -212,8 +212,23 @@ TEST(TransferTest, CopyRecordFromDerivedToBase) {
       // [[p]]
     }
   )";
+  auto SyntheticFieldCallback = [](QualType Ty) -> llvm::StringMap<QualType> {
+    CXXRecordDecl *ADecl = nullptr;
+    if (Ty.getAsString() == "A")
+      ADecl = Ty->getAsCXXRecordDecl();
+    else if (Ty.getAsString() == "B")
+      ADecl = Ty->getAsCXXRecordDecl()
+                  ->bases_begin()
+                  ->getType()
+                  ->getAsCXXRecordDecl();
+    else
+      return {};
+    QualType IntTy = getFieldNamed(ADecl, "i")->getType();
+    return {{"synth_int", IntTy}};
+  };
+  // Copy derived to base class.
----------------
martinboehme wrote:

It doesn't happen in the analyzed code snippet at all. There's an explicit call to `copyRecord()` below -- this is where the copy happens.

This test is trying to be a "unit test" for `copyRecord()`. The code snippet is only there to establish the record type; the actual behavior under test is the `copyRecord()` call (i.e. I'm intentionally calling this function directly rather than trying to trigger a call to `copyRecord()` indirectly).

This is different from how we usually do tests in the framework (and in compilers in general), but I think if we _can_ do unit tests relatively easily (and in this case we can), it makes the tests clearer because you don't need to reason about "how does this code trigger the behavior we want to test".

(There are also code-level tests for copy construction and assignment that exercise `copyRecord()`; we obviously want to have these too.)

https://github.com/llvm/llvm-project/pull/85064


More information about the cfe-commits mailing list