1

I'm exploring ways to alter the AST of a C/C++ code (e.g., rename a node, add a new variable) and apply these changes to the source file.

I did a lot of reading here in SO and in Eclipse forums. However I didn't find a minimal working example.

It seems that the correct way to make changes in an AST is by using the ASTRewrite class.

A similar question was asked in SO a few months ago, but it is still pending.

Here is where I'm stuck at the moment:

//get the factory
INodeFactory nodeFactory = myAST.getASTNodeFactory();

//create a new function declarator
IASTNode n = nodeFactory.newFunctionDeclarator(nodeFactory.newName("testMe"));

//get the rewriter                  
ASTRewrite rewriter = ASTRewrite.create(mainAST);

//replace node with n, node is not null
rewriter.replace(node, n, null);

//make the changes              
Change c = rewriter.rewriteAST();
c.perform(new NullProgressMonitor());

When I run this code snippet, I get a

java.lang.NoClassDefFoundError: org/eclipse/ltk/core/refactoring/Change

Any hints are appreciated.

Community
  • 1
  • 1
STiGMa
  • 906
  • 3
  • 11
  • 24
  • What happens if you leave out the replace(...) statement? – Ira Baxter Nov 26 '16 at 12:23
  • It doesn't sound like you are committed to CDT. You might consider our DMS with its C++ front end; it has a mature program modification capability. See http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html – Ira Baxter Nov 26 '16 at 12:24
  • @IraBaxter, the exception is thrown at the last command c.perform(...) – STiGMa Nov 26 '16 at 12:52
  • @IraBaxter thank you for the suggestion. I will take a look. It's a research projects so ideally I'd like open source software. How about DMS? Or is there any academic license? – STiGMa Nov 26 '16 at 12:55
  • 1
    How are you running the CDT code? It sounds like you are running it with main. The upvoted answer provided in your linked question does say that. Can you say how your question differs? – Jonah Graham Nov 26 '16 at 18:07
  • @JonahGraham, it's an Eclipse plugin project (with a main and lots). So, that answer doesn't help at all; in fact, I am not sure that it answered the other guy's question. – STiGMa Nov 26 '16 at 18:14
  • 1
    Have you written your own main? If so, it does not sound like an OSGi bundle that is being launched allowing OSGi to bootstrap and therefore find the classes. – Jonah Graham Nov 26 '16 at 18:44
  • No, eclipse plugins have their own main and their own way to be launched. – STiGMa Nov 26 '16 at 19:52
  • DMS doesn't have a "free" academic license. It does have a research license which is modest compared to the time and effort it saves a researcher, even on an academic grant salary. Contact the company for details. – Ira Baxter Dec 01 '16 at 02:40
  • @IraBaxter Offtopic, but out of curiosity, what does DMS use to parse C++ code? A hand-crafted parser? EDG? clang? – HighCommander4 Dec 14 '16 at 07:36
  • Unlike EDG or Clang that use a hand-crafted parser, DMS uses the grammar from the ANSI standard augmented with additional rules to handle what GCC and MS offer, backed up by a GLR parser. See http://stackoverflow.com/questions/243383/why-cant-c-be-parsed-with-a-lr1-parser/1004737#1004737 and http://stackoverflow.com/questions/17388771/get-human-readable-ast-from-c-code/17393852#17393852 – Ira Baxter Dec 14 '16 at 07:50

0 Answers0