【发布时间】:2019-07-24 10:07:02
【问题描述】:
我正在尝试遍历所有函数定义并从中提取信息。我必须遍历函数体中的所有语句,并根据类型执行特定函数。
目前我有一个丑陋的 if-else 块。有没有更优雅的方法来做到这一点?
void FunctionMatcher::processStatement(const clang::Stmt *statement) {
string type = statement->getStmtClassName();
if (type == "ReturnStmt") {
auto rs = dyn_cast<const ReturnStmt *>(statement);
processReturnStmt(rs);
} else if (type == "WhileStmt") {
auto ws = dyn_cast<WhileStmt>(statement);
processWhileStmt(ws);
} else if (type == "ForStmt") {
auto fs = dyn_cast<const ForStmt *>(statement);
processForStmt(fs);
} else if (type == "IfStmt") {
auto is = dyn_cast<const IfStmt *>(statement);
processIfStmt(is);
} else if (type == "SwitchStmt") {
auto ss = dyn_cast<const SwitchStmt *>(statement);
processSwitchStmt(ss);
} else if (type == "CompoundStmt") {
auto cs = dyn_cast<const CompoundStmt *>(statement);
for (auto child : cs->children())
processStatement(child);
} else {
// ...
}
【问题讨论】:
-
@KostasRim 这就是我用来提取函数的东西。现在我想遍历正文中的语句并处理它们。
-
看起来像 visitor pattern 的经典用例,不是吗?
-
@G.M.我需要用 accept 方法扩展 Stmt 类,不是吗?这些是库类。
标签: c++ clang abstract-syntax-tree clang++