Env.setValue(Loc, Env.create<PointerValue>(*ThisPointeeLoc));
}
+ void VisitCXXNewExpr(const CXXNewExpr *S) {
+ auto &Loc = Env.createStorageLocation(*S);
+ Env.setStorageLocation(*S, Loc);
+ if (Value *Val = Env.createValue(S->getType()))
+ Env.setValue(Loc, *Val);
+ }
+
+ void VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
+ // Empty method.
+ // We consciously don't do anything on deletes. Diagnosing double deletes
+ // (for example) should be done by a specific analysis, not by the
+ // framework.
+ }
+
void VisitReturnStmt(const ReturnStmt *S) {
if (!Env.getAnalysisOptions().ContextSensitiveOpts)
return;
});
}
+TEST(TransferTest, NewExpressions) {
+ std::string Code = R"(
+ void target() {
+ int *p = new int(42);
+ // [[after_new]]
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const Environment &Env =
+ getEnvironmentAtAnnotation(Results, "after_new");
+
+ auto &P = getValueForDecl<PointerValue>(ASTCtx, Env, "p");
+
+ EXPECT_THAT(Env.getValue(P.getPointeeLoc()), NotNull());
+ });
+}
+
+TEST(TransferTest, NewExpressions_Structs) {
+ std::string Code = R"(
+ struct Inner {
+ int InnerField;
+ };
+
+ struct Outer {
+ Inner OuterField;
+ };
+
+ void target() {
+ Outer *p = new Outer;
+ // Access the fields to make sure the analysis actually generates children
+ // for them in the `AggregateStorageLoc` and `StructValue`.
+ p->OuterField.InnerField;
+ // [[after_new]]
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const Environment &Env =
+ getEnvironmentAtAnnotation(Results, "after_new");
+
+ const ValueDecl *OuterField = findValueDecl(ASTCtx, "OuterField");
+ const ValueDecl *InnerField = findValueDecl(ASTCtx, "InnerField");
+
+ auto &P = getValueForDecl<PointerValue>(ASTCtx, Env, "p");
+
+ auto &OuterLoc = cast<AggregateStorageLocation>(P.getPointeeLoc());
+ auto &OuterFieldLoc =
+ cast<AggregateStorageLocation>(OuterLoc.getChild(*OuterField));
+ auto &InnerFieldLoc = OuterFieldLoc.getChild(*InnerField);
+
+ // Values for the struct and all fields exist after the new.
+ EXPECT_THAT(Env.getValue(OuterLoc), NotNull());
+ EXPECT_THAT(Env.getValue(OuterFieldLoc), NotNull());
+ EXPECT_THAT(Env.getValue(InnerFieldLoc), NotNull());
+ });
+}
+
} // namespace