Bug 507252 - NPE in quickfixes if file is open in non-CEditor (has no open AST)
Summary: NPE in quickfixes if file is open in non-CEditor (has no open AST)
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: 9.1.0   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: CDT Codan Inbox CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-11-08 17:15 EST by Jonah Graham CLA
Modified: 2017-05-30 05:40 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonah Graham CLA 2016-11-08 17:15:01 EST
Trying to run a quick fix (for example creating local variable on code below) when the file is only open in a non-CEditor it causes an NPE because the editor has no TU and the quick fix fails to run.

void func() {
    x = 2;
}


The method org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution.getTranslationUnitViaEditor(IMarker) is not documented as being able to return null, but it can. 

Here is the stack trace.

!ENTRY org.eclipse.cdt.codan.internal.ui.cxx 4 1 2016-11-08 22:07:14.810
!MESSAGE Error
!STACK 0
java.lang.NullPointerException
	at org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateLocalVariable.modifyAST(QuickFixCreateLocalVariable.java:41)
	at org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix.apply(AbstractAstRewriteQuickFix.java:52)
	at org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution.run(AbstractCodanCMarkerResolution.java:96)
	at org.eclipse.ui.internal.views.markers.QuickFixPage.lambda$3(QuickFixPage.java:490)
	at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:437)
Comment 1 Nathan Ridge CLA 2016-11-10 03:05:06 EST
Some notes on reproducing this:

  - One way to open a C++ file in a non-CEditor is to right-click
    on it in Project Explorer -> Open With... -> Text Editor.

  - One way to run a quick fix on a file not open in a CEditor is
    open the Problems view, right click on the problem in question,
    and select Quick fix.

A couple of additional observations: 

  - You can perform a quick fix from the Problems view even if the 
    affected file isn't open at all. In such a case, the affected 
    file is opened in a CEditor.

  - A file can be open simultaneously in a CEditor and a non-
    CEditor.

Therefore, it seems like an appropriate fix would be to ignore the non-CEditor in which the file is open, and open it again in a CEditor.
Comment 2 Dominic Scharfe CLA 2016-11-11 07:02:21 EST
I'd suggest to modify the method org.eclipse.cdt.codan.ui.CodanEditorUtility.openInEditor

Currently it opens the editor via:
IEditorPart part = page.openEditor(new FileEditorInput(efile), desc.getId());

org.eclipse.ui.IWorkbenchPage provides a (IMHO) more suitable method:
openEditor(final IEditorInput input,final String editorId, final boolean activate, final int matchFlags)

So I think what we need to call is:
IEditorPart part = page.openEditor(new FileEditorInput(efile), desc.getId(), true, IWorkbenchPage.MATCH_INPUT|IWorkbenchPage.MATCH_ID);

What I'm not sure about is, what to do if the file is not inside the Eclipse workspace. In this case IDE.openEditorOnFileStore(page, fileStore) is used.
Comment 3 Nathan Ridge CLA 2017-05-30 02:24:35 EDT
(In reply to Dominic Scharfe from comment #2)
> What I'm not sure about is, what to do if the file is not inside the Eclipse
> workspace. In this case IDE.openEditorOnFileStore(page, fileStore) is used.

Can that even happen? A marker resolution applies to an IMarker, and markers are associated with an IResource. Can you have an IResource that's not part of the workspace?
Comment 4 Jonah Graham CLA 2017-05-30 05:40:32 EDT
(In reply to Nathan Ridge from comment #3)
> (In reply to Dominic Scharfe from comment #2)
> > What I'm not sure about is, what to do if the file is not inside the Eclipse
> > workspace. In this case IDE.openEditorOnFileStore(page, fileStore) is used.
> 
> Can that even happen? A marker resolution applies to an IMarker, and markers
> are associated with an IResource. Can you have an IResource that's not part
> of the workspace?

You can't have an IResource outside the workspace, but you can have a marker that is. The marker for items outside the workspace have an IResource of the workspace root and an extra attribute ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION with that external location. As the location is workspace, double-clicking on the error marker does not open the file. However a action is on new context menu "Open external location" --> org.eclipse.cdt.internal.ui.util.OpenExternalProblemAction