[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [cdt-dev] Reading formal parameters from AST
|
It'd be easier to resolve the binding for the function main:
IFunction f= (IFunction) declarator.getName().resolveBinding();
>From there you have access to the function type:
IFunctionType ft= f.getType();
Then you can check return type and parameter types:
ft.getReturnType(); ft.getParameterTypes(); // or similar
Markus.
> -----Original Message-----
> From: cdt-dev-bounces@xxxxxxxxxxx
> [mailto:cdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Hagge, Nils
> Sent: Wednesday, January 20, 2010 9:36 AM
> To: CDT General developers list.
> Subject: [cdt-dev] Reading formal parameters from AST
> Importance: Low
>
> 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;
> }
> @Override
> public int visit(IASTDeclarator declarator)
> {
> System.out.println(i++ + " \""
> + declarator.getName() + "\"");
> if(declarator instanceof
> IASTFunctionDeclarator &&
> declarator.getName().toString().equals("main"))
> node[0] = declarator;
>
> return PROCESS_CONTINUE;
> }
> });
>
> if(node[0] == null)
> status.addFatalError("No 'main'
> function found in project.");
> else
> {
> IASTFunctionDeclarator mainDecl =
> (IASTFunctionDeclarator)node[0];
> if(mainDecl instanceof
> IASTStandardFunctionDeclarator)
> {
> IASTParameterDeclaration[] params =
>
> ((IASTStandardFunctionDeclarator)mainDecl).getParameters();
>
> IASTName argCountParam =
> params.length >= 1 ? params[0].getDeclarator().getName() : null;
> IASTName argValueParam =
> params.length >= 2 ? params[1].getDeclarator().getName() : null;
>
> try
> {
> if(argCountParam == null)
>
> status.addFatalError("'main' function lacks formal parameter
> for argument counter.");
> else
> {
> IVariable
> argCount = (IVariable)argCountParam.resolveBinding();
> String argcType
> = ASTTypeUtil.getType(argCount.getType());
>
> if(!"int".equals(argcType))
>
> 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.");
> else
> {
> 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)
> {
>
> status.addFatalError(e1.getLocalizedMessage());
> e1.printStackTrace();
> }
>
> }
>
>
> 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>
> <http://www.siemens.com/automation>
> http://www.siemens.com/automation <http://www.siemens.com/automation>
>
> 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
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/cdt-dev
>