[clang][dataflow] Add a test for not explicitly initialized fields in aggregate initi...
authorMartin Braenne <mboehme@google.com>
Mon, 17 Jul 2023 06:27:42 +0000 (06:27 +0000)
committerMartin Braenne <mboehme@google.com>
Mon, 17 Jul 2023 07:26:08 +0000 (07:26 +0000)
Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D155074

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

index 5a5540c..e280045 100644 (file)
@@ -2944,6 +2944,39 @@ TEST(TransferTest, AggregateInitializationReferenceField) {
       });
 }
 
+TEST(TransferTest, AggregateInitialization_NotExplicitlyInitializedField) {
+  std::string Code = R"(
+    struct S {
+      int i1;
+      int i2;
+    };
+
+    void target(int i) {
+      S s = { i };
+      /*[[p]]*/
+    }
+  )";
+  runDataflow(
+      Code,
+      [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+         ASTContext &ASTCtx) {
+        const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+        const ValueDecl *I1FieldDecl = findValueDecl(ASTCtx, "i1");
+        const ValueDecl *I2FieldDecl = findValueDecl(ASTCtx, "i2");
+
+        auto &SLoc = getLocForDecl<AggregateStorageLocation>(ASTCtx, Env, "s");
+
+        auto &IValue = getValueForDecl<IntegerValue>(ASTCtx, Env, "i");
+        auto &I1Value =
+            *cast<IntegerValue>(getFieldValue(&SLoc, *I1FieldDecl, Env));
+        EXPECT_EQ(&I1Value, &IValue);
+        auto &I2Value =
+            *cast<IntegerValue>(getFieldValue(&SLoc, *I2FieldDecl, Env));
+        EXPECT_NE(&I2Value, &IValue);
+      });
+}
+
 TEST(TransferTest, AssignToUnionMember) {
   std::string Code = R"(
     union A {