title: Is there an elegant way to traverse Clang AST Statements?
body: <p>I am trying to traverse all function definitions and extract information from them. I have to iterate over all statements in the function body, and depending on the type, execute a specific function.</p>
  
  <p>For the moment I have an ugly if-else block. Is there a more elegant way to do this?</p>
  
  <pre><code>void FunctionMatcher::processStatement(const clang::Stmt *statement) {
      string type = statement-&gt;getStmtClassName();
      if (type == "ReturnStmt") {
          auto rs = dyn_cast&lt;const ReturnStmt *&gt;(statement);
          processReturnStmt(rs);
      } else if (type == "WhileStmt") {
          auto ws = dyn_cast&lt;WhileStmt&gt;(statement);
          processWhileStmt(ws);
      } else if (type == "ForStmt") {
          auto fs = dyn_cast&lt;const ForStmt *&gt;(statement);
          processForStmt(fs);
      } else if (type == "IfStmt") {
          auto is = dyn_cast&lt;const IfStmt *&gt;(statement);
          processIfStmt(is);
      } else if (type == "SwitchStmt") {
          auto ss = dyn_cast&lt;const SwitchStmt *&gt;(statement);
          processSwitchStmt(ss);
      } else if (type == "CompoundStmt") {
          auto cs = dyn_cast&lt;const CompoundStmt *&gt;(statement);
          for (auto child : cs-&gt;children())
              processStatement(child);
      } else {
        // ...
      }
  </code></pre>