[clang] 6ab900f - [clang][dataflow] Add support for new expressions.
Martin Braenne via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 17 21:11:55 PDT 2023
Author: Martin Braenne
Date: 2023-04-18T04:11:43Z
New Revision: 6ab900f8746e7d8e24afafb5886a40801f6799f4
URL: https://github.com/llvm/llvm-project/commit/6ab900f8746e7d8e24afafb5886a40801f6799f4
DIFF: https://github.com/llvm/llvm-project/commit/6ab900f8746e7d8e24afafb5886a40801f6799f4.diff
LOG: [clang][dataflow] Add support for new expressions.
Reviewed By: xazax.hun, gribozavr2
Differential Revision: https://reviews.llvm.org/D147698
Added:
Modified:
clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index aa8fe907b167..1d273e77ef0b 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -469,6 +469,20 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
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;
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 1bb772a93bda..8f02161834dc 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5170,4 +5170,66 @@ TEST(TransferTest, NoReturnFunctionInsideShortCircuitedBooleanOp) {
});
}
+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
More information about the cfe-commits
mailing list