[clang] 1e9b4fc - [clang][dataflow] Various refactorings in TypeErasedDataflowAnalysisTest.cpp

Martin Braenne via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 11 21:52:48 PDT 2023


Author: Martin Braenne
Date: 2023-07-12T04:52:27Z
New Revision: 1e9b4fc1dcf27ae43477efe0329f738e4419871b

URL: https://github.com/llvm/llvm-project/commit/1e9b4fc1dcf27ae43477efe0329f738e4419871b
DIFF: https://github.com/llvm/llvm-project/commit/1e9b4fc1dcf27ae43477efe0329f738e4419871b.diff

LOG: [clang][dataflow] Various refactorings in TypeErasedDataflowAnalysisTest.cpp

These simplify the code in their own right, but they are also useful in that they minimize the number of changes that will need to be made when then API of `AggregateStorageLocation` and `StructValue` changes as part of the migration to strict handling of value categories (see https://discourse.llvm.org/t/70086).

Depends On D154949

Reviewed By: xazax.hun, gribozavr2

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

Added: 
    

Modified: 
    clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
index 473750ad7a6cb4..d811aed8fddd99 100644
--- a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
@@ -370,6 +370,13 @@ TEST_F(NoreturnDestructorTest, ConditionalOperatorNestedBranchReturns) {
   // FIXME: Called functions at point `p` should contain only "foo".
 }
 
+StructValue &createNewStructValue(AggregateStorageLocation &Loc,
+                                  Environment &Env) {
+  auto &Val = *cast<StructValue>(Env.createValue(Loc.getType()));
+  Env.setValue(Loc, Val);
+  return Val;
+}
+
 // Models an analysis that uses flow conditions.
 class SpecialBoolAnalysis final
     : public DataflowAnalysis<SpecialBoolAnalysis, NoopLattice> {
@@ -390,23 +397,18 @@ class SpecialBoolAnalysis final
     if (const auto *E = selectFirst<CXXConstructExpr>(
             "call", match(cxxConstructExpr(HasSpecialBoolType).bind("call"), *S,
                           getASTContext()))) {
-      auto &ConstructorVal = *Env.createValue(E->getType());
-      ConstructorVal.setProperty("is_set", Env.getBoolLiteralValue(false));
-      Env.setValue(*Env.getStorageLocation(*E, SkipPast::None), ConstructorVal);
+      cast<StructValue>(Env.getValueStrict(*E))
+          ->setProperty("is_set", Env.getBoolLiteralValue(false));
     } else if (const auto *E = selectFirst<CXXMemberCallExpr>(
                    "call", match(cxxMemberCallExpr(callee(cxxMethodDecl(ofClass(
                                                        SpecialBoolRecordDecl))))
                                      .bind("call"),
                                  *S, getASTContext()))) {
-      auto *Object = E->getImplicitObjectArgument();
-      assert(Object != nullptr);
-
-      auto *ObjectLoc = getImplicitObjectLocation(*E, Env);
-      assert(ObjectLoc != nullptr);
+      auto &ObjectLoc =
+          *cast<AggregateStorageLocation>(getImplicitObjectLocation(*E, Env));
 
-      auto &ConstructorVal = *Env.createValue(Object->getType());
-      ConstructorVal.setProperty("is_set", Env.getBoolLiteralValue(true));
-      Env.setValue(*ObjectLoc, ConstructorVal);
+      createNewStructValue(ObjectLoc, Env)
+          .setProperty("is_set", Env.getBoolLiteralValue(true));
     }
   }
 
@@ -551,21 +553,19 @@ class OptionalIntAnalysis final
         *S, getASTContext());
     if (const auto *E = selectFirst<CXXConstructExpr>(
             "construct", Matches)) {
-      auto &ConstructorVal = *Env.createValue(E->getType());
-      ConstructorVal.setProperty("has_value", Env.getBoolLiteralValue(false));
-      Env.setValue(*Env.getStorageLocation(*E, SkipPast::None), ConstructorVal);
+      cast<StructValue>(Env.getValueStrict(*E))
+          ->setProperty("has_value", Env.getBoolLiteralValue(false));
     } else if (const auto *E =
                    selectFirst<CXXOperatorCallExpr>("operator", Matches)) {
       assert(E->getNumArgs() > 0);
       auto *Object = E->getArg(0);
       assert(Object != nullptr);
 
-      auto *ObjectLoc = Env.getStorageLocation(*Object, SkipPast::Reference);
-      assert(ObjectLoc != nullptr);
+      auto &ObjectLoc = *cast<AggregateStorageLocation>(
+          Env.getStorageLocation(*Object, SkipPast::Reference));
 
-      auto &ConstructorVal = *Env.createValue(Object->getType());
-      ConstructorVal.setProperty("has_value", Env.getBoolLiteralValue(true));
-      Env.setValue(*ObjectLoc, ConstructorVal);
+      createNewStructValue(ObjectLoc, Env)
+          .setProperty("has_value", Env.getBoolLiteralValue(true));
     }
   }
 
@@ -1227,9 +1227,7 @@ class TopAnalysis final : public DataflowAnalysis<TopAnalysis, NoopLattice> {
         match(callExpr(callee(functionDecl(hasName("makeTop")))).bind("top"),
               *S, getASTContext());
     if (const auto *E = selectFirst<CallExpr>("top", Matches)) {
-      auto &Loc = Env.createStorageLocation(*E);
-      Env.setValue(Loc, Env.makeTopBoolValue());
-      Env.setStorageLocation(*E, Loc);
+      Env.setValueStrict(*E, Env.makeTopBoolValue());
     }
   }
 


        


More information about the cfe-commits mailing list