mboehme updated this revision to Diff 512434.
mboehme added a comment.
Eliminate specific handling of delete expressions.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D147698/new/
https://reviews.llvm.org/D147698
Files:
clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5170,4 +5170,66 @@
});
}
+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
Index: clang/lib/Analysis/FlowSensitive/Transfer.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -469,6 +469,20 @@
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;
Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5170,4 +5170,66 @@
});
}
+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
Index: clang/lib/Analysis/FlowSensitive/Transfer.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -469,6 +469,20 @@
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;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits