[clang] [clang][dataflow] Refactor `PropagateResultObject()` with a switch statement. (PR #88865)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 04:03:01 PDT 2024


https://github.com/martinboehme updated https://github.com/llvm/llvm-project/pull/88865

>From 440ace1337ed7a06286a4455e3e6e428d14d847d Mon Sep 17 00:00:00 2001
From: Martin Braenne <mboehme at google.com>
Date: Tue, 16 Apr 2024 09:52:35 +0000
Subject: [PATCH] [clang][dataflow] Refactor `PropagateResultObject()` with a
 switch statement.

See also discussion in #88726.
---
 .../FlowSensitive/DataflowEnvironment.cpp     | 68 +++++++++++--------
 1 file changed, 41 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index ee2581143e1141..7ef6b9d1df5182 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -500,21 +500,43 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
 
     ResultObjectMap[E] = Loc;
 
+    switch (E->getStmtClass()) {
     // The following AST node kinds are "original initializers": They are the
     // lowest-level AST node that initializes a given object, and nothing
     // below them can initialize the same object (or part of it).
-    if (isa<CXXConstructExpr>(E) || isa<CallExpr>(E) || isa<LambdaExpr>(E) ||
-        isa<CXXDefaultArgExpr>(E) || isa<CXXDefaultInitExpr>(E) ||
-        isa<CXXStdInitializerListExpr>(E)) {
+    case Stmt::CXXConstructExprClass:
+    case Stmt::CallExprClass:
+    case Stmt::LambdaExprClass:
+    case Stmt::CXXDefaultArgExprClass:
+    case Stmt::CXXDefaultInitExprClass:
+    case Stmt::CXXStdInitializerListExprClass:
+      return;
+    case Stmt::BinaryOperatorClass: {
+      auto *Op = cast<BinaryOperator>(E);
+      if (Op->getOpcode() == BO_Cmp) {
+        // Builtin `<=>` returns a `std::strong_ordering` object. We
+        // consider this to be an "original" initializer too (see above).
+        return;
+      }
+      if (Op->isCommaOp()) {
+        PropagateResultObject(Op->getRHS(), Loc);
+        return;
+      }
+      // We don't expect any other binary operators to produce a record
+      // prvalue, so if we get here, we've hit some case we don't know
+      // about.
+      assert(false);
       return;
     }
-    if (auto *Op = dyn_cast<BinaryOperator>(E);
-        Op && Op->getOpcode() == BO_Cmp) {
-      // Builtin `<=>` returns a `std::strong_ordering` object.
+    case Stmt::BinaryConditionalOperatorClass:
+    case Stmt::ConditionalOperatorClass: {
+      auto *Cond = cast<AbstractConditionalOperator>(E);
+      PropagateResultObject(Cond->getTrueExpr(), Loc);
+      PropagateResultObject(Cond->getFalseExpr(), Loc);
       return;
     }
-
-    if (auto *InitList = dyn_cast<InitListExpr>(E)) {
+    case Stmt::InitListExprClass: {
+      auto *InitList = cast<InitListExpr>(E);
       if (!InitList->isSemanticForm())
         return;
       if (InitList->isTransparent()) {
@@ -544,28 +566,20 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
       }
       return;
     }
-
-    if (auto *Op = dyn_cast<BinaryOperator>(E); Op && Op->isCommaOp()) {
-      PropagateResultObject(Op->getRHS(), Loc);
+    default: {
+      // All other expression nodes that propagate a record prvalue should
+      // have exactly one child.
+      SmallVector<Stmt *, 1> Children(E->child_begin(), E->child_end());
+      LLVM_DEBUG({
+        if (Children.size() != 1)
+          E->dump();
+      });
+      assert(Children.size() == 1);
+      for (Stmt *S : Children)
+        PropagateResultObject(cast<Expr>(S), Loc);
       return;
     }
-
-    if (auto *Cond = dyn_cast<AbstractConditionalOperator>(E)) {
-      PropagateResultObject(Cond->getTrueExpr(), Loc);
-      PropagateResultObject(Cond->getFalseExpr(), Loc);
-      return;
     }
-
-    // All other expression nodes that propagate a record prvalue should have
-    // exactly one child.
-    SmallVector<Stmt *, 1> Children(E->child_begin(), E->child_end());
-    LLVM_DEBUG({
-      if (Children.size() != 1)
-        E->dump();
-    });
-    assert(Children.size() == 1);
-    for (Stmt *S : Children)
-      PropagateResultObject(cast<Expr>(S), Loc);
   }
 
 private:



More information about the cfe-commits mailing list