Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams x = 5;

I used eclipse cdt to generate the AST, and traverse on it, so this is the code of the traversed class

public class RuleChk extends AbstractRule {
    public RuleChk(IASTTranslationUnit ast) {
        super("RuleChk", false, ast);
        shouldVisitDeclarations = true;
        shouldVisitParameterDeclarations = true;
    @Override
    public int visit(IASTSimpleDeclaration simpleDecl) {
        //if this node has init, e.g: x = 5, do business
        if(VisitorUtil.containNode(simpleDecl, CASTExpressionStatement){
           // Now I have the x = 5 node, 
           // I want to get the reference node of it's declaration
           // I mean (int x;) node
           IASTNode declNode = ?????
        return super.visit(parameterDeclaration);

what I want to visit the node that only has assignation(Initialization) and get the reference of declaration node for that varaiable.

I'm not sure how VisitorUtil works (it's not from the CDT code), but I assume it gives you a way to access the the found node. So:

  • Given the IASTExpressionStatement node that was found, use IASTExpression.getExpression() to get the contained expression.

  • See if it's an IASTBinaryExpression, and that is getOperator() is IASTBinaryExpression.op_assign.

  • Use IASTBinaryExpression.getOperand1() to get the assignment expression's left subexpression. Check that it's an IASTIdExpression, and get the variable it names via IASTIdExpression.getName().

  • Now that you have the name, use IASTName.resolveBinding() to get the variable's binding. This is the variable's representation in the semantic program model.

  • To find the variable's definition, use IASTTranslationUnit.getDefinitionsInAST(IBinding) if you only want it to look in the current file, or IASTTranslationUnit.getDefinitions(IBinding) if you want it to look in included header files as well (the latter requires the project to be indexed). The IASTTranslationUnit can be obtained from any IASTNode via IASTNode.getTranslationUnit().

  • I have done this before, but I need to get the declaration specifiers for the name, for the above example I want to return ( int ) – Mostafa Hassan Nov 19, 2017 at 12:03 @MostafaHassan Assuming the declaration is in the same file, getDefinitionInAST() will give you an IASTName. You can examine the surrounding AST to find the decl-specifiers. For example, the name's parent would be an IASTDeclarator, its parent an IASTSimpleDeclaration, and then you can use IASTSimpleDeclaration.getDeclSpecifier() to get the decl-specifier. – HighCommander4 Nov 19, 2017 at 18:37

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.