Skip to content

Commit 440ace1

Browse files
committed
[clang][dataflow] Refactor PropagateResultObject() with a switch statement.
See also discussion in #88726.
1 parent 3c6f91e commit 440ace1

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -500,21 +500,43 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
500500

501501
ResultObjectMap[E] = Loc;
502502

503+
switch (E->getStmtClass()) {
503504
// The following AST node kinds are "original initializers": They are the
504505
// lowest-level AST node that initializes a given object, and nothing
505506
// below them can initialize the same object (or part of it).
506-
if (isa<CXXConstructExpr>(E) || isa<CallExpr>(E) || isa<LambdaExpr>(E) ||
507-
isa<CXXDefaultArgExpr>(E) || isa<CXXDefaultInitExpr>(E) ||
508-
isa<CXXStdInitializerListExpr>(E)) {
507+
case Stmt::CXXConstructExprClass:
508+
case Stmt::CallExprClass:
509+
case Stmt::LambdaExprClass:
510+
case Stmt::CXXDefaultArgExprClass:
511+
case Stmt::CXXDefaultInitExprClass:
512+
case Stmt::CXXStdInitializerListExprClass:
513+
return;
514+
case Stmt::BinaryOperatorClass: {
515+
auto *Op = cast<BinaryOperator>(E);
516+
if (Op->getOpcode() == BO_Cmp) {
517+
// Builtin `<=>` returns a `std::strong_ordering` object. We
518+
// consider this to be an "original" initializer too (see above).
519+
return;
520+
}
521+
if (Op->isCommaOp()) {
522+
PropagateResultObject(Op->getRHS(), Loc);
523+
return;
524+
}
525+
// We don't expect any other binary operators to produce a record
526+
// prvalue, so if we get here, we've hit some case we don't know
527+
// about.
528+
assert(false);
509529
return;
510530
}
511-
if (auto *Op = dyn_cast<BinaryOperator>(E);
512-
Op && Op->getOpcode() == BO_Cmp) {
513-
// Builtin `<=>` returns a `std::strong_ordering` object.
531+
case Stmt::BinaryConditionalOperatorClass:
532+
case Stmt::ConditionalOperatorClass: {
533+
auto *Cond = cast<AbstractConditionalOperator>(E);
534+
PropagateResultObject(Cond->getTrueExpr(), Loc);
535+
PropagateResultObject(Cond->getFalseExpr(), Loc);
514536
return;
515537
}
516-
517-
if (auto *InitList = dyn_cast<InitListExpr>(E)) {
538+
case Stmt::InitListExprClass: {
539+
auto *InitList = cast<InitListExpr>(E);
518540
if (!InitList->isSemanticForm())
519541
return;
520542
if (InitList->isTransparent()) {
@@ -544,28 +566,20 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
544566
}
545567
return;
546568
}
547-
548-
if (auto *Op = dyn_cast<BinaryOperator>(E); Op && Op->isCommaOp()) {
549-
PropagateResultObject(Op->getRHS(), Loc);
569+
default: {
570+
// All other expression nodes that propagate a record prvalue should
571+
// have exactly one child.
572+
SmallVector<Stmt *, 1> Children(E->child_begin(), E->child_end());
573+
LLVM_DEBUG({
574+
if (Children.size() != 1)
575+
E->dump();
576+
});
577+
assert(Children.size() == 1);
578+
for (Stmt *S : Children)
579+
PropagateResultObject(cast<Expr>(S), Loc);
550580
return;
551581
}
552-
553-
if (auto *Cond = dyn_cast<AbstractConditionalOperator>(E)) {
554-
PropagateResultObject(Cond->getTrueExpr(), Loc);
555-
PropagateResultObject(Cond->getFalseExpr(), Loc);
556-
return;
557582
}
558-
559-
// All other expression nodes that propagate a record prvalue should have
560-
// exactly one child.
561-
SmallVector<Stmt *, 1> Children(E->child_begin(), E->child_end());
562-
LLVM_DEBUG({
563-
if (Children.size() != 1)
564-
E->dump();
565-
});
566-
assert(Children.size() == 1);
567-
for (Stmt *S : Children)
568-
PropagateResultObject(cast<Expr>(S), Loc);
569583
}
570584

571585
private:

0 commit comments

Comments
 (0)