Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » CDT Ast Parser NPE
CDT Ast Parser NPE [message #1841120] Tue, 04 May 2021 12:37 Go to next message
Johannes Koch is currently offline Johannes KochFriend
Messages: 2
Registered: May 2021
Junior Member
I am trying to develop a Eclipse plugin which is rewriting the AST of some .c source files.

Currently I am simply trying to add a simple statement once into a IFile (_file) obtained from a project of the active workspace. See the following code:

Map<String, String> definedSymbols = new HashMap<String, String>();
String[] includePaths = new String[0];
IScannerInfo info = new ScannerInfo(definedSymbols, includePaths);
IParserLogService log = new DefaultLogService();
IncludeFileContentProvider emptyIncludes = IncludeFileContentProvider.getEmptyFilesProvider();
int opts = 8;
final IASTTranslationUnit tu =  GCCLanguage.getDefault().getASTTranslationUnit(FileContent.create(_file), info, emptyIncludes, null, opts, log);
final ASTRewrite rw = ASTRewrite.create(tu);
tu.accept(new ASTVisitor(true){
		public int visit(IASTStatement statement){
		        if(once) {
			    once= false;
			    rw.insertBefore(statement.getParent(), statement, rw.createLiteralNode("int i = 3;"), null);
Change c = rw.rewriteAST();
c.perform(new NullProgressMonitor());

The line:
Change c = rw.rewriteAST();

Results in a NPE:

at org.eclipse.cdt.internal.formatter.ChangeFormatter.formatChangedCode(
at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(
at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(
at org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteAST(
at org.eclipse.cdt.core.dom.rewrite.ASTRewrite.rewriteAST(
at sheaperdexecuter.AstParser.parse(
at org.eclipse.swt.widgets.TypedListener.handleEvent(
at org.eclipse.swt.widgets.EventTable.sendEvent(
at org.eclipse.swt.widgets.Display.sendEvent(
at org.eclipse.swt.widgets.Widget.sendEvent(
at org.eclipse.swt.widgets.Display.runDeferredEvents(
at org.eclipse.swt.widgets.Display.readAndDispatch(
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$
at org.eclipse.core.databinding.observable.Realm.runWithDefault(
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(
at org.eclipse.ui.internal.Workbench.lambda$3(
at org.eclipse.core.databinding.observable.Realm.runWithDefault(
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(
at org.eclipse.ui.internal.ide.application.IDEApplication.start(
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
at java.base/java.lang.reflect.Method.invoke(
at org.eclipse.equinox.launcher.Main.invokeFramework(
at org.eclipse.equinox.launcher.Main.basicRun(
at org.eclipse.equinox.launcher.Main.main(

I found similar problems online where 'Jonah Graham' helped, but the solution was to not run the code as Java application but inside an Eclipse plugin (or osgi framework in general).

Maybe someone can help and guide me to a solution.
(As I am a new user I cannot provide the SO links with the similar problems)


Re: CDT Ast Parser NPE [message #1841158 is a reply to message #1841120] Wed, 05 May 2021 09:42 Go to previous message
Johannes Koch is currently offline Johannes KochFriend
Messages: 2
Registered: May 2021
Junior Member
I could solve my problem and will add my solution as also available on SO. (

The problem was due to the fact that I added CDT via .jar using the Runtime configuration tab of the plugin. Initiallly I was not able to add CDT via the Dependencies configuration tab of the plugin.
My calls to the CDT environment (CCorePlugin, CoreModel, etc.) ended up not being related to the CDT running in the executing eclipse. Therefore I got NPE's on CoreModel and stuff.
I solved the problem by cloning the CDT source repo ( and importing the projects to my Eclipse Committers IDE. (As explained here:

Afterwards I was able to add CDT to the Dependencies configuration tab of the plugin:

After the dependency has been added the NPE's were gone and I could access the correct Instances of the workspace related models etc..

I hope this helps someone as it took me some hours to get the Eclipse plugin with CDT bootstrapped.
Previous Topic:Eclipse(mingw64) want to use WinMain instead main
Next Topic:Is there a way to stop Eclipse from auto-selecting project references?
Goto Forum:

Current Time: Wed Sep 28 22:53:15 GMT 2022

Powered by FUDForum. Page generated in 0.01853 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top