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){
		@Override
		public int visit(IASTStatement statement){
		        if(once) {
			    once= false;
			    rw.insertBefore(statement.getParent(), statement, rw.createLiteralNode("int i = 3;"), null);
			}
			return PROCESS_CONTINUE;
		}
});
Change c = rw.rewriteAST();
c.perform(new NullProgressMonitor());



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


Results in a NPE:
Quote:

java.lang.NullPointerException
at org.eclipse.cdt.internal.formatter.ChangeFormatter.formatChangedCode(ChangeFormatter.java:95)
at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:119)
at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:107)
at org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteAST(ASTRewriteAnalyzer.java:29)
at org.eclipse.cdt.core.dom.rewrite.ASTRewrite.rewriteAST(ASTRewrite.java:213)
at sheaperdexecuter.AstParser.parse(AstParser.java:155)
at sheaperdexecuter.parts.SheaperdView$3.mouseDown(SheaperdView.java:140)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:196)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1461)
at org.eclipse.equinox.launcher.Main.main(Main.java:1434)


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)

Regards,
JK

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. (https://stackoverflow.com/questions/67385661/eclipse-cdt-astrewrite-npe/67398617#67398617)

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 (https://git.eclipse.org/r/cdt/org.eclipse.cdt.git) and importing the projects to my Eclipse Committers IDE. (As explained here: https://wiki.eclipse.org/Getting_started_with_CDT_development#Setting_up_the_Sources)

Afterwards I was able to add CDT to the Dependencies configuration tab of the plugin:
https://www1.xup.in/exec/ximg.php?fid=71177974

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 Dec 08 13:55:49 GMT 2021

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

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

Back to the top