0
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.

Jeremy Kuah
  • 519
  • 1
  • 6
  • 18
  • 1
    I suggest you to do a http://sscce.org/ example. Usually, when trying to do a self contained program, there is a highly chance that you will find the problem by yourself. To me, the problem seems to reside somewhere not shown here. – Amadeus Jun 30 '16 at 10:06
  • Please create a [mcve] which will also display your *use* of the class(es) involved. – Angew is no longer proud of SO Jun 30 '16 at 12:11
  • I'll try to create a complete example. In the meantime, would anyone be familiar with clang TraverseDecl? Does that invoke only on the node that you pass into it then recursively visits decls within that node? (e.g. Visitor->TraverseDecl(methodDecl)) – Jeremy Kuah Jul 01 '16 at 02:15
  • Thanks for the help @Amadeus and @Angew, I've did something similar to what Amadeus said. The problem was indeed not in the code above. Long story short, my VisitVarDecl was visiting ParmVarDecl too and that was messing up my map. Had to check if `VarDecl isa` and do a `return true;` if it is to skip running the method and that solved the issue. I guess we all learn something new everyday ;) Cheers! – Jeremy Kuah Jul 01 '16 at 02:57

0 Answers0