有可能与libTooling
. 如果您已经找到DeclRefExpr
引用全局变量的节点,则可以将 AST 向上传递给它们的FunctionDecl
父节点。
另一方面,走上 AST 需要 Clang 构建从节点到其父节点的映射(这对于大型翻译单元来说可能相当昂贵)。在这里,我整理了一个简短的解决方案,它只找到引用全局变量的函数并打印它们的名称:
class GlobalVariableFinder final
: public RecursiveASTVisitor<GlobalVariableFinder> {
public:
static bool find(FunctionDecl *CandidateFunction) {
GlobalVariableFinder ActualFinder;
ActualFinder.TraverseDecl(CandidateFunction);
return ActualFinder.Found;
}
bool VisitDeclRefExpr(DeclRefExpr *SymbolUse) {
// we are interested only in variables
if (auto *Declaration = dyn_cast<VarDecl>(SymbolUse->getDecl())) {
Found = Declaration->hasGlobalStorage();
// if we found one global variable use, there is no need in traversing
// this function any further
if (Found) return false;
}
return true;
}
private:
bool Found = false;
};
class VisitingASTConsumer final
: public ASTConsumer,
public RecursiveASTVisitor<VisitingASTConsumer> {
public:
void HandleTranslationUnit(ASTContext &C) {
this->TraverseTranslationUnitDecl(Context->getTranslationUnitDecl());
}
bool VisitFunctionDecl(FunctionDecl *CandidateFunction) {
if (GlobalVariableFinder::find(CandidateFunction)) {
llvm::errs() << CandidateFunction->getQualifiedNameAsString() << "\n";
}
return true;
}
};
如果要存储全局变量引用,则可能需要修改GlobalVariableFinder
类以包含其他逻辑。
以下解决方案在此代码片段上产生以下输出:
int Var;
int display()
{
Var = 10;
return Var;
}
int foo() {
return Var;
}
int bar() {
return foo();
}
int main()
{
display();
return 0;
}
display
foo
您会注意到它仅包含在语法上使用全局变量的函数。如果您希望算法也能找到bar
,您需要构建目标程序的调用图,并通过图的反向边(即我们的示例中的fromfoo
到)传播有关全局变量的信息。bar
我希望这些信息对你有用。与 Clang 一起愉快地黑客攻击!