[PATCH] D117667: [clang][dataflow] Add a transfer function for conditional operator

Stanislav Gatev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 19 08:26:03 PST 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8e53ae3d3719: [clang][dataflow] Add a transfer function for conditional operator (authored by sgatev).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117667/new/

https://reviews.llvm.org/D117667

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
@@ -1788,4 +1788,44 @@
       });
 }
 
+TEST_F(TransferTest, VarDeclInitAssignConditionalOperator) {
+  std::string Code = R"(
+    struct A {};
+
+    void target(A Foo, A Bar, bool Cond) {
+      A Baz = Cond ?  Foo : Bar;
+      /*[[p]]*/
+    }
+  )";
+  runDataflow(
+      Code, [](llvm::ArrayRef<
+                   std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
+                   Results,
+               ASTContext &ASTCtx) {
+        ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
+        const Environment &Env = Results[0].second.Env;
+
+        const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+        ASSERT_THAT(FooDecl, NotNull());
+
+        const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+        ASSERT_THAT(BarDecl, NotNull());
+
+        const ValueDecl *BazDecl = findValueDecl(ASTCtx, "Baz");
+        ASSERT_THAT(BazDecl, NotNull());
+
+        const auto *FooVal =
+            cast<StructValue>(Env.getValue(*FooDecl, SkipPast::None));
+        const auto *BarVal =
+            cast<StructValue>(Env.getValue(*BarDecl, SkipPast::None));
+
+        const auto *BazVal =
+            dyn_cast<StructValue>(Env.getValue(*BazDecl, SkipPast::None));
+        ASSERT_THAT(BazVal, NotNull());
+
+        EXPECT_NE(BazVal, FooVal);
+        EXPECT_NE(BazVal, BarVal);
+      });
+}
+
 } // namespace
Index: clang/lib/Analysis/FlowSensitive/Transfer.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -404,6 +404,16 @@
     }
   }
 
+  void VisitConditionalOperator(const ConditionalOperator *S) {
+    // FIXME: Revisit this once flow conditions are added to the framework. For
+    // `a = b ? c : d` we can add `b => a == c && !b => a == d` to the flow
+    // condition.
+    auto &Loc = Env.createStorageLocation(*S);
+    Env.setStorageLocation(*S, Loc);
+    if (Value *Val = Env.createValue(S->getType()))
+      Env.setValue(Loc, *Val);
+  }
+
   // FIXME: Add support for:
   // - CXXBoolLiteralExpr
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117667.401255.patch
Type: text/x-patch
Size: 2343 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220119/d475907e/attachment.bin>


More information about the cfe-commits mailing list