我正在使用基于示例Clang ASTFrontendActions 示例的LibTooling 为 clang 4.0 编写工具。给定当前语句 stmt,我想在 AST 中获取它的直接父级。因此,我尝试使用以下代码转储 stmt 的所有父级(用于测试目的):
bool VisitStmt(Stmt *s) {
cout <<"Trying to get parents \n";
const Stmt currentStmt = *s;
const auto& parents = Context->getParents(currentStmt);
auto it = Context->getParents(currentStmt).begin();
if(it == Context->getParents(currentStmt).end())
cout<< "parents not found\n";
cout<<"parents size "<< parents.size() <<": \n";
if (!parents.empty()){
for (int i = 0; i< parents.size(); i++ ){
cout<<"parent at "<< i <<": \n";
const Stmt* parentStmt = parents[i].get<Stmt>();
parentStmt->dump();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Context在ASTContext …
有没有一种方法可以检查scala中foreach循环中的条件。这个例子是我要遍历一个整数数组并对正数做一些任意的数学运算。
val arr = Array(-1,2,3,0,-7,4) //The array
//What I want to do but doesn't work
arr.foreach{if(/*This condition is true*/)
{/*Do some math and print the answer*/}}
//Expected answer for division by six is 0.333333333 \n 0.5 \n 0.666666667
//Which is 2/6, 3/6 and 4/6 respectively as they appear in the array
Run Code Online (Sandbox Code Playgroud)
我知道如何使用普通的for循环和if语句来执行此操作,但是我想使用它,因为我想摆脱Java。
谢谢