mboehme created this revision. Herald added subscribers: martong, xazax.hun. Herald added a project: All. mboehme requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
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 <https://reviews.llvm.org/D154949> Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D154952 Files: clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp Index: clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp @@ -370,6 +370,13 @@ // 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 @@ 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 @@ *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 @@ 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()); } }
Index: clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp @@ -370,6 +370,13 @@ // 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 @@ 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 @@ *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 @@ 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()); } }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits