nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Avoid the anti-pattern of:

  if dyn_cast<X>:
    ...
  elif dyn_cast<Y>:
    ...
  elif dyn_cast<Z>:
    ...

And simply switch on the statement class, as is done elsewhere in this
class. These cases are for classes with no shared inheritance lineage
beyond all inheriting from clang::Stmt.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155506

Files:
  clang/lib/Sema/JumpDiagnostics.cpp


Index: clang/lib/Sema/JumpDiagnostics.cpp
===================================================================
--- clang/lib/Sema/JumpDiagnostics.cpp
+++ clang/lib/Sema/JumpDiagnostics.cpp
@@ -643,8 +643,9 @@
   while (!Jumps.empty()) {
     Stmt *Jump = Jumps.pop_back_val();
 
-    // With a goto,
-    if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
+    switch (Jump->getStmtClass()) {
+    case Stmt::GotoStmtClass: {
+      auto *GS = cast<GotoStmt>(Jump);
       // The label may not have a statement if it's coming from inline MS ASM.
       if (GS->getLabel()->getStmt()) {
         CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
@@ -653,10 +654,10 @@
                   diag::warn_cxx98_compat_goto_into_protected_scope);
       }
       CheckGotoStmt(GS);
-      continue;
+      break;
     }
-
-    if (auto *G = dyn_cast<GCCAsmStmt>(Jump)) {
+    case Stmt::GCCAsmStmtClass: {
+      auto *G = cast<GCCAsmStmt>(Jump);
       for (AddrLabelExpr *L : G->labels()) {
         LabelDecl *LD = L->getLabel();
         unsigned JumpScope = LabelAndGotoScopes[G];
@@ -664,33 +665,38 @@
         if (JumpScope != TargetScope)
           DiagnoseIndirectOrAsmJump(G, JumpScope, LD, TargetScope);
       }
-      continue;
+      break;
     }
-
-    // We only get indirect gotos here when they have a constant target.
-    if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
+    case Stmt::IndirectGotoStmtClass: {
+      // We only get indirect gotos here when they have a constant target.
+      auto *IGS = dyn_cast<IndirectGotoStmt>(Jump);
       LabelDecl *Target = IGS->getConstantTarget();
       CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
                 diag::err_goto_into_protected_scope,
                 diag::ext_goto_into_protected_scope,
                 diag::warn_cxx98_compat_goto_into_protected_scope);
-      continue;
+      break;
     }
-
-    SwitchStmt *SS = cast<SwitchStmt>(Jump);
-    for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
-         SC = SC->getNextSwitchCase()) {
-      if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
-        continue;
-      SourceLocation Loc;
-      if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
-        Loc = CS->getBeginLoc();
-      else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
-        Loc = DS->getBeginLoc();
-      else
-        Loc = SC->getBeginLoc();
-      CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
-                diag::warn_cxx98_compat_switch_into_protected_scope);
+    case Stmt::SwitchStmtClass: {
+      auto *SS = cast<SwitchStmt>(Jump);
+      for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
+          SC = SC->getNextSwitchCase()) {
+        if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
+          continue;
+        SourceLocation Loc;
+        if (const auto *CS = dyn_cast<CaseStmt>(SC))
+          Loc = CS->getBeginLoc();
+        else if (const auto *DS = dyn_cast<DefaultStmt>(SC))
+          Loc = DS->getBeginLoc();
+        else
+          Loc = SC->getBeginLoc();
+        CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
+            diag::warn_cxx98_compat_switch_into_protected_scope);
+      }
+      break;
+    }
+    default:
+      break;
     }
   }
 }


Index: clang/lib/Sema/JumpDiagnostics.cpp
===================================================================
--- clang/lib/Sema/JumpDiagnostics.cpp
+++ clang/lib/Sema/JumpDiagnostics.cpp
@@ -643,8 +643,9 @@
   while (!Jumps.empty()) {
     Stmt *Jump = Jumps.pop_back_val();
 
-    // With a goto,
-    if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
+    switch (Jump->getStmtClass()) {
+    case Stmt::GotoStmtClass: {
+      auto *GS = cast<GotoStmt>(Jump);
       // The label may not have a statement if it's coming from inline MS ASM.
       if (GS->getLabel()->getStmt()) {
         CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
@@ -653,10 +654,10 @@
                   diag::warn_cxx98_compat_goto_into_protected_scope);
       }
       CheckGotoStmt(GS);
-      continue;
+      break;
     }
-
-    if (auto *G = dyn_cast<GCCAsmStmt>(Jump)) {
+    case Stmt::GCCAsmStmtClass: {
+      auto *G = cast<GCCAsmStmt>(Jump);
       for (AddrLabelExpr *L : G->labels()) {
         LabelDecl *LD = L->getLabel();
         unsigned JumpScope = LabelAndGotoScopes[G];
@@ -664,33 +665,38 @@
         if (JumpScope != TargetScope)
           DiagnoseIndirectOrAsmJump(G, JumpScope, LD, TargetScope);
       }
-      continue;
+      break;
     }
-
-    // We only get indirect gotos here when they have a constant target.
-    if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
+    case Stmt::IndirectGotoStmtClass: {
+      // We only get indirect gotos here when they have a constant target.
+      auto *IGS = dyn_cast<IndirectGotoStmt>(Jump);
       LabelDecl *Target = IGS->getConstantTarget();
       CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
                 diag::err_goto_into_protected_scope,
                 diag::ext_goto_into_protected_scope,
                 diag::warn_cxx98_compat_goto_into_protected_scope);
-      continue;
+      break;
     }
-
-    SwitchStmt *SS = cast<SwitchStmt>(Jump);
-    for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
-         SC = SC->getNextSwitchCase()) {
-      if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
-        continue;
-      SourceLocation Loc;
-      if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
-        Loc = CS->getBeginLoc();
-      else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
-        Loc = DS->getBeginLoc();
-      else
-        Loc = SC->getBeginLoc();
-      CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
-                diag::warn_cxx98_compat_switch_into_protected_scope);
+    case Stmt::SwitchStmtClass: {
+      auto *SS = cast<SwitchStmt>(Jump);
+      for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
+          SC = SC->getNextSwitchCase()) {
+        if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
+          continue;
+        SourceLocation Loc;
+        if (const auto *CS = dyn_cast<CaseStmt>(SC))
+          Loc = CS->getBeginLoc();
+        else if (const auto *DS = dyn_cast<DefaultStmt>(SC))
+          Loc = DS->getBeginLoc();
+        else
+          Loc = SC->getBeginLoc();
+        CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
+            diag::warn_cxx98_compat_switch_into_protected_scope);
+      }
+      break;
+    }
+    default:
+      break;
     }
   }
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to