c++ - Determining Variable Accesses Using Clang -
i'm building clang tool goes through c/c++ program , records variable references along whether read or modified. i'm using astmatchers match expressions match variable declarations.
my code follows.
for astmatcher:
finder->addmatcher(declrefexpr(hasdeclaration(vardecl(isexpansioninmainfile()).bind(types[var_call])),hasancestor(functiondecl().bind(types[caller_var]))).bind(types[var_expr]), this); for run(...) portion of matcher:
if (const vardecl *dec = result.nodes.getnodeas<clang::vardecl>(types[var_call])){ //if variable reference has been found. auto *caller = result.nodes.getnodeas<clang::declaratordecl>(types[caller_var]); auto *expr = result.nodes.getnodeas<clang::declrefexpr>(types[var_expr]); //todo: detect simple variable read/writes. } ignoring aliasing (since want simple, direct accesses), there way this?
i did see this previous stackoverflow question answers focused on aliasing.
example:
#include <iostream> using namespace std; int main() { int test = 0; int test2 = 1; //variable modification. test = test2; return 0; } analyzing sample c++ program detect test being modified , test2 being read from.
Comments
Post a Comment