Search code examples
eclipse-plugineclipse-cdt

eclipse CDT Plugin development : How to get the class name which has a declaration


I'm developing an eclipse plugin based on CDT API.

Suppose I've following C++ code

   class EventEnum
   {
   public:
      enum e { 
         E_CompleteDisconnectSuccess = 1, 
         E_CreateBtAdapterNoSuccess = 2, 
         E_CreateBtAdapterSuccess = 3, 
      };
   };

Using following ASTVisitor visitor method I can find the enum declaration

public int visit(IASTDeclaration declaration) {
    if (declaration instanceof IASTSimpleDeclaration) {

        IASTDeclSpecifier specifier = ((IASTSimpleDeclaration)declaration).getDeclSpecifier();

        if (specifier instanceof IASTEnumerationSpecifier) {
            IASTEnumerationSpecifier enumSpecifier = (IASTEnumerationSpecifier)specifier;
            // Get the current enumeration name
            String enumerationName = enumSpecifier.getName().toString();

            System.out.println("Found enum : " + enumerationName);
        }
    }
    return PROCESS_CONTINUE;
}

Question : How can I get the class name which contains the found enum declaration, in my case it will be EventEnum ?


Solution

  • Found the answer on my own, probably for someone it will be usefull, so I'm posting it here

    if (enumSpecifier.getParent() instanceof CPPASTSimpleDeclaration)
    {
        if (enumSpecifier.getParent().getParent() instanceof CPPASTCompositeTypeSpecifier)
        {
            CPPASTCompositeTypeSpecifier firstLevelClass = (CPPASTCompositeTypeSpecifier)enumSpecifier.getParent().getParent();
            return firstLevelClass.getName().toString();
        }
    }