Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Reading formal parameters from AST

Dear friends,
I currently try to get familar with the CDT-AST model. I want to implement a refactoring function that adds
some code to a project's main function. First, I want to check if the main function has the desired formal parameters.
In order to find the 'main' and and to check the type of the formal parameters I wrote the following code.
I have the feeling that it is a little bit to complex. Especially, the try-catch block that
uses Strings to check the parameter types appears cumbersome to me.

Any comments and ideas to improve the code are appreciated.

            final IASTNode node[] = new IASTNode[1];
            unit.accept(new CPPASTVisitor() {
			int i = 0;
				shouldVisitDeclarators = true; 
			public int visit(IASTDeclarator declarator) 
				System.out.println(i++ + " \"" + declarator.getName() + "\"");
				if(declarator instanceof IASTFunctionDeclarator && declarator.getName().toString().equals("main"))
					node[0] = declarator;
		if(node[0] == null)
			status.addFatalError("No 'main' function found in project.");
			IASTFunctionDeclarator mainDecl = (IASTFunctionDeclarator)node[0];
			if(mainDecl instanceof IASTStandardFunctionDeclarator)
				IASTParameterDeclaration[] params = 
				IASTName argCountParam = params.length >= 1 ? params[0].getDeclarator().getName() : null;
				IASTName argValueParam = params.length >= 2 ? params[1].getDeclarator().getName() : null;
					if(argCountParam == null)
						status.addFatalError("'main' function lacks formal parameter for argument counter.");
						IVariable argCount = (IVariable)argCountParam.resolveBinding();
						String argcType = ASTTypeUtil.getType(argCount.getType());
							status.addFatalError("First parameter of 'main' function is expected to be of type 'int'.");
					if(argValueParam == null)
						status.addFatalError("'main' function lacks formal parameter for argument values.");
						IVariable argValue = (IVariable)argValueParam.resolveBinding();
						String argvType = ASTTypeUtil.getType(argValue.getType());
						if(!"char * *".equals(argvType))
							status.addFatalError("Second parameter of 'main' function is expected to be of type 'char **'.");
				catch(DOMException e1)
Mit freundlichen Grüßen / Best regards

Dr. Nils Hagge
Projektleiter Vorfeld

Siemens AG
Industry Sector, I IA&DT ATS 13
Telefon +49 911 895-3484
Telefax +49 911 895-153484
mailto:nils.hagge@xxxxxxxxxxx <mailto:nils.hagge@xxxxxxxxxxx> 
<> <> 

Siemens Aktiengesellschaft: Chairman of the Supervisory Board: Gerhard Cromme
Managing Board: Peter Loescher, Chairman, President and Chief Executive Officer; 
Wolfgang Dehen, Heinrich Hiesinger, Joe Kaeser, Jim Reid-Anderson, Hermann Requardt,
Siegfried Russwurm, Peter Y. Solmssen
Registered offices: Berlin and Munich; 
Commercial registries: Berlin Charlottenburg, HRB 12300, Munich, HRB 6684
WEEE-Reg.-No. DE 23691322

Back to the top