class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor> {
private:
ASTContext *Context;
std::map<std::string, std::string> localMap;
public:
explicit MyASTVisitor(ASTContext *Context) : Context(Context) {}
bool VisitObjCMethodDecl (ObjCMethodDecl *D) {
localMap["test"] = "sample information";
// isInLocalMap("test") always returns true here
return true;
}
bool VisitBinaryOperator (BinaryOperator *binaryOperator) {
if (!binaryOperator->isAssignmentOp()) {
return true;
}
// My RHSexpr has DeclRefExpr inside that returns "test" on getNameAsString()
Expr *RHSexpr = binaryOperator->getRHS();
isNameInStatement(RHSExpr);
}
bool isNameInStatement (Stmt *stmt) {
// Recursively check statements within this subtree
// isInLocalMap("test") always returns false here
}
bool isInLocalMap (std::string name) {
return !(localMap.find(name) == localMap.end());
}
}
This is a short sample code that I hope will help to explain what I want to ask.
In the above scenario, I'm always passing an ObjCMethodDecl into MyASTVisitor for analysis. The first Visit Method it will hit is always VisitObjCMethodDecl and I've added "test" into my localMap. I verified that it is inside my localMap using isInLocalMap("test") within the VisitObjCMethodDecl.
However, when checking the contents localMap using isInLocalMap("test") within my method isNameInStatement, it always returns false. Manually iterating through localMap tells me that I have nothing inside my map.
Am I doing something wrongly here? Is it a memory issue in C++ or something?
I certainly hope this is not a case of it being ran as a multi-threaded program and that it hits VisitBinaryOperator way before VisitObjCMethodDecl
I came from background in Java. If MyASTVisitor is an Object, everything should still be contained inside the object right? I should be able to reference the localMap from anywhere inside the object.