[PATCH] D147698: [clang][dataflow] Add support for new expressions.
Martin Böhme via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 17 21:11:57 PDT 2023
This revision was automatically updated to reflect the committed changes.
mboehme marked 3 inline comments as done.
Closed by commit rG6ab900f8746e: [clang][dataflow] Add support for new expressions. (authored by mboehme).
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147698.514517.patch
Type: text/x-patch
Size: 3195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230418/c278a625/attachment.bin>
More information about the cfe-commits
mailing list