Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] how to add a statement into an empty .CPP file
  • From: Ming Cheng <chengm349@xxxxxxxxxxx>
  • Date: Tue, 18 Feb 2020 08:53:04 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; 1; spf=none; dmarc=none; dkim=none; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed;; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Pxt22xuGmsmlZjNRCMK73LZ9yMTyQXdQx0DQbMGq/Xs=; b=n4Kbiro0pgwqrEGRuZfwkVmux8TNKaUb2ncvKrezAG4CofyEe+8eJ8bRB6vd2pfoCaIBlGa6n7aDuHgDPgp37lcS9UJ5bkyRthtsuf4eNjzz65sfdPf2qhmDIBDgExlHl7I14punFGlurUMtm45BjCNr3PuqY4rMCeSNpi4ExtoBjXBQbMKObXIkN5drTJ4iDJPKPZjy3jQ8lROGWFgapDv20uHsBVfUp7asGiILgFZPax5sw4Jnhb0DGvWlMPfku3Fep2NYgwtVthpE9657MahE/T6Kp8oWzgldkzRuk/8UBmr3OzIFu1OEa0P8D7fATEtFarP8ywWa2GNyNfexQQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901;; cv=none; b=O6CUUSFRpvhh3gpDIDM/FY0Mf4HEPtV1ZrO7eNYTvH2oMqAwJhGBcMJ8excyxs3uokIF9buTOs8xGy7aE702Jg5rQ2ORTOviUHqGI+TCiNrcIUCCDoybuqScFmjbciJAzTZ48GvvgD+Zq3/lTMfAs3dnsHx7shHDuIZbN65u/O5NvB4MOyGGkPZtdRWu0TrnrO9f8yW9Na++tmAdK1rNqrzsGrVmjcKeCClnyGoG5GR4z+6DlwG07U5aPyG1efu+Oz9442cheUuamtBQgygBssrjCLd1lxvGtNKbOyijoopS9o4yC+o+3GIQrrYVhYbFlHFGZ5FDXlyD1HkhPKNYLQ==
  • Delivered-to: cdt-dev@xxxxxxxxxxx
  • List-archive: <>
  • List-help: <>
  • List-subscribe: <>, <>
  • List-unsubscribe: <>, <>
  • Thread-index: AQHV5WR3HRtSvq/jIEKUv795wi97MqggDmP5gACPUUCAAAVkIw==
  • Thread-topic: how to add a statement into an empty .CPP file


really did not receive any reply.

thanks and I can see break after adding r.insertBefore(ast, null, breakStatement, null); .

From: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> on behalf of Corbat Thomas <thomas.corbat@xxxxxx>
Sent: Tuesday, February 18, 2020 8:20 AM
To: CDT General developers list. <cdt-dev@xxxxxxxxxxx>
Subject: Re: [cdt-dev] how to add a statement into an empty .CPP file

You didn’t receive my reply, did you?


Otherwise, I wrote this:


Hi Ming Cheng


You need to actually insert/replace/remove a node through the rewriter in order to see something happening. Just creating a new break statement with a factory will not be represented anywhere. For example you could create a new declaration, through the node factory, and insert it at the beginning of the translation unit:

r.insertBefore(ast, null, newDeclaration, null);


You might be able to find other applications of the ASTRewrite infrastructure in the refactoring part of the UI plug-in. I hope this helps.






From: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> On Behalf Of Ming Cheng
Sent: Dienstag, 18. Februar 2020 00:48
To: cdt-dev@xxxxxxxxxxx
Subject: Re: [cdt-dev] how to add a statement into an empty .CPP file


really hope someone can give a detail guide.


From: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> on behalf of Ming Cheng <chengm349@xxxxxxxxxxx>
Sent: Monday, February 17, 2020 7:37 AM
To: cdt-dev@xxxxxxxxxxx <cdt-dev@xxxxxxxxxxx>
Subject: [cdt-dev] how to add a statement into an empty .CPP file


private void buildMainFunction(IFile file) {

        try {

            IASTTranslationUnit ast = CurrentGui.getInstance().getGui().getAST(file);

            ASTRewrite r = ASTRewrite.create( ast );

            IASTBreakStatement breakStatement = ast.getASTNodeFactory().newBreakStatement();

            Change c = r.rewriteAST();

            c.perform(new NullProgressMonitor());            

        } catch (CoreException e) {





PS. file was created successfully. But after this buildMainFunction() call, the file is still empty. If I try ASTRewrite replace(), my program will complain org.umlet.plugin null.


    public IASTTranslationUnit getAST(IFile file) {

        try {

            ITranslationUnit tu= (ITranslationUnit) CoreModel.getDefault().create(file);

            IASTTranslationUnit ast = tu.getAST();

            return ast;

        } catch (CoreException e) {


            return null;



Back to the top