1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

Handle potential null pointers in ASTRunnable.runOnAST()

This commit is contained in:
Markus Schorn 2007-07-27 09:28:16 +00:00
parent 9cedcf1a8e
commit 94bee24a59
5 changed files with 39 additions and 27 deletions

View file

@ -89,19 +89,21 @@ public class CElementHyperlinkDetector implements IHyperlinkDetector {
try { try {
IStatus status= ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() { IStatus status= ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() {
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) { public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
IASTName[] selectedNames= if (ast != null) {
lang.getSelectedNames(ast, selection.getOffset(), selection.getLength()); IASTName[] selectedNames=
lang.getSelectedNames(ast, selection.getOffset(), selection.getLength());
IRegion linkRegion; IRegion linkRegion;
if(selectedNames.length > 0 && selectedNames[0] != null) { // found a name if(selectedNames.length > 0 && selectedNames[0] != null) { // found a name
linkRegion = new Region(selection.getOffset(), selection.getLength()); linkRegion = new Region(selection.getOffset(), selection.getLength());
} }
else { // check if we are in an include statement else { // check if we are in an include statement
linkRegion = matchIncludeStatement(ast, selection); linkRegion = matchIncludeStatement(ast, selection);
} }
if(linkRegion != null) if(linkRegion != null)
result[0]= new CElementHyperlink(linkRegion, openAction); result[0]= new CElementHyperlink(linkRegion, openAction);
}
return Status.OK_STATUS; return Status.OK_STATUS;
} }
}); });

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation * Anton Leherbauer (Wind River Systems) - initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.editor; package org.eclipse.cdt.internal.ui.editor;
@ -104,11 +105,13 @@ public class ToggleSourceAndHeaderAction extends TextEditorAction {
shouldVisitDeclarators= true; shouldVisitDeclarators= true;
} }
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) { public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
fIndex= ast.getIndex(); if (ast != null) {
fFilePath= Path.fromOSString(ast.getFilePath()); fIndex= ast.getIndex();
fMap= new HashMap(); fFilePath= Path.fromOSString(ast.getFilePath());
if (fIndex != null) { fMap= new HashMap();
ast.accept(this); if (fIndex != null) {
ast.accept(this);
}
} }
return Status.OK_STATUS; return Status.OK_STATUS;
} }

View file

@ -6,8 +6,8 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* QNX - Initial API and implementation * QNX - Initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.search; package org.eclipse.cdt.internal.ui.search;
@ -48,12 +48,14 @@ public class PDOMSearchTextSelectionQuery extends PDOMSearchQuery {
protected IStatus runWithIndex(final IIndex index, IProgressMonitor monitor) { protected IStatus runWithIndex(final IIndex index, IProgressMonitor monitor) {
return ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_YES, monitor, new ASTRunnable() { return ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_YES, monitor, new ASTRunnable() {
public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException { public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException {
IASTName[] names = language.getSelectedNames(ast, selection.getOffset(), selection.getLength()); if (ast != null) {
if (names != null) { IASTName[] names = language.getSelectedNames(ast, selection.getOffset(), selection.getLength());
for (int i = 0; i < names.length; ++i) { if (names != null) {
IBinding binding = names[i].resolveBinding(); for (int i = 0; i < names.length; ++i) {
if (binding != null) IBinding binding = names[i].resolveBinding();
createMatches(index, binding); if (binding != null)
createMatches(index, binding);
}
} }
} }
return Status.OK_STATUS; return Status.OK_STATUS;

View file

@ -114,6 +114,9 @@ public class OpenDeclarationsAction extends SelectionParseAction {
} }
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException { public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException {
if (ast == null) {
return Status.OK_STATUS;
}
int selectionStart = selNode.getOffset(); int selectionStart = selNode.getOffset();
int selectionLength = selNode.getLength(); int selectionLength = selNode.getLength();

View file

@ -339,9 +339,11 @@ public class IndexUI {
final IASTName[] result= {null}; final IASTName[] result= {null};
ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() { ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() {
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) { public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
FindNameForSelectionVisitor finder= new FindNameForSelectionVisitor(ast.getFilePath(), selectionStart, selectionLength); if (ast != null) {
ast.accept(finder); FindNameForSelectionVisitor finder= new FindNameForSelectionVisitor(ast.getFilePath(), selectionStart, selectionLength);
result[0]= finder.getSelectedName(); ast.accept(finder);
result[0]= finder.getSelectedName();
}
return Status.OK_STATUS; return Status.OK_STATUS;
} }
}); });