[clang] [clang][dataflow] Add context-sensitive test for returning a record by value. (PR #84317)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 7 04:59:31 PST 2024


https://github.com/martinboehme created https://github.com/llvm/llvm-project/pull/84317

I'm making some changes to `Environment::getResultObjectLocation()`, with the
ultimate goal of eliminating `RecordValue` entirely, and I'd like to make sure
I don't break this behavior (and I've realized we don't have a test for it yet).


>From 360c4ba88473579d5033c27343aba1c82c60ea64 Mon Sep 17 00:00:00 2001
From: Martin Braenne <mboehme at google.com>
Date: Thu, 7 Mar 2024 12:58:59 +0000
Subject: [PATCH] [clang][dataflow] Add context-sensitive test for returning a
 record by value.

I'm making some changes to `Environment::getResultObjectLocation()`, with the
ultimate goal of eliminating `RecordValue` entirely, and I'd like to make sure
I don't break this behavior (and I've realized we don't have a test for it yet).
---
 .../Analysis/FlowSensitive/TransferTest.cpp   | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index f534ccb1254701..33762bc4c13f25 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5672,6 +5672,39 @@ TEST(TransferTest, ContextSensitiveReturnInt) {
       {BuiltinOptions{ContextSensitiveOptions{}}});
 }
 
+TEST(TransferTest, ContextSensitiveReturnRecord) {
+  std::string Code = R"(
+    struct S {
+      bool B;
+    };
+
+    S makeS(bool BVal) { return {BVal}; }
+
+    void target() {
+      S FalseS = makeS(false);
+      S TrueS = makeS(true);
+      // [[p]]
+    }
+  )";
+  runDataflow(
+      Code,
+      [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+         ASTContext &ASTCtx) {
+        const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+        auto &FalseSLoc =
+            getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "FalseS");
+        auto &TrueSLoc =
+            getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "TrueS");
+
+        EXPECT_EQ(getFieldValue(&FalseSLoc, "B", ASTCtx, Env),
+                  &Env.getBoolLiteralValue(false));
+        EXPECT_EQ(getFieldValue(&TrueSLoc, "B", ASTCtx, Env),
+                  &Env.getBoolLiteralValue(true));
+      },
+      {BuiltinOptions{ContextSensitiveOptions{}}});
+}
+
 TEST(TransferTest, ContextSensitiveMethodLiteral) {
   std::string Code = R"(
     class MyClass {



More information about the cfe-commits mailing list