Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] How to identify a class in a *.cpp file?

You should create a visitor that extends ASTVisitor and attach it to the
IASTTranslationUnit of each C++ file. Implement visit(IASTDeclaration)
method as below. It will find all the classes.


import org.eclipse.cdt.core.dom.ast.ASTVisitor;

class ClassFinder extends ASTVisitor

	public int visitDeclaration(IASTDeclaration decl) {
		try {
			if ((decl instanceof IASTSimpleDeclaration) &&
decl.isPartOfTranslationUnitFile()) {
				IASTSimpleDeclaration simpDecl = (IASTSimpleDeclaration) decl;
				IASTDeclSpecifier declSpecifier = simpDecl.getDeclSpecifier();
				if (declSpecifier instanceof IASTCompositeTypeSpecifier) {
					IASTCompositeTypeSpecifier compTypeSpecifier 
						= (IASTCompositeTypeSpecifier)declSpecifier;
					int key = compTypeSpecifier.getKey();
				        if (key == ICPPASTCompositeTypeSpecifier.k_class) {
						IASTName astName = compTypeSpecifier.getName();
						IBinding binding = astName.resolveBinding();
						if (binding instanceof ICPPClassType) {
							ICPPClassType cppClass = (ICPPClassType)binding;
                                                        //... you found the

View this message in context:*.cpp-file--tp23946997p23955760.html
Sent from the Eclipse CDT - Development mailing list archive at

Back to the top