From a44996a2003fb289d77aa9fb71290e2602533f75 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Thu, 27 Apr 2017 01:54:34 -0400 Subject: [PATCH] Bug 513428 - Ensure PDOM implementations of IASTFileLocation.getFileName() do not return null Change-Id: I1adb158bd8efc23300bf5c212d6ed4f532a19205 --- .../cdt/core/dom/ast/IASTFileLocation.java | 2 +- .../core/pdom/dom/PDOMASTFileLocation.java | 63 +++++++++++++++++++ .../cdt/internal/core/pdom/dom/PDOMMacro.java | 38 +++-------- .../pdom/dom/PDOMMacroDefinitionName.java | 2 +- .../core/pdom/dom/PDOMMacroReferenceName.java | 34 ++-------- .../cdt/internal/core/pdom/dom/PDOMName.java | 34 ++-------- 6 files changed, 84 insertions(+), 89 deletions(-) create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTFileLocation.java diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java index 36174deb3d5..7ba1fb7526b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java @@ -18,7 +18,7 @@ package org.eclipse.cdt.core.dom.ast; */ public interface IASTFileLocation extends IASTNodeLocation { /** - * The name of the file. + * The name of the file. Should not be null. * * @return the name of the file */ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTFileLocation.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTFileLocation.java new file mode 100644 index 00000000000..e7026a49fe3 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTFileLocation.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2017 Nathan Ridge. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.cdt.internal.core.pdom.dom; + +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; + +/** + * Implementation of IASTFileLocation for use by PDOM types. + * This implementation just stores the fields which need to be computed by the caller + * at constructor time. + */ +public class PDOMASTFileLocation implements IASTFileLocation { + private String fFilename; + private int fNodeOffset; + private int fNodeLength; + + public PDOMASTFileLocation(String filename, int nodeOffset, int nodeLength) { + fFilename = filename; + fNodeOffset = nodeOffset; + fNodeLength = nodeLength; + } + + @Override + public IASTFileLocation asFileLocation() { + return this; + } + + @Override + public String getFileName() { + return fFilename; + } + + @Override + public int getNodeOffset() { + return fNodeOffset; + } + + @Override + public int getNodeLength() { + return fNodeLength; + } + + @Override + public int getStartingLineNumber() { + return 0; + } + + @Override + public int getEndingLineNumber() { + return 0; + } + + @Override + public IASTPreprocessorIncludeStatement getContextInclusionStatement() { + return null; + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacro.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacro.java index 08c2ca1f6e2..894701f1f33 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacro.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacro.java @@ -20,7 +20,6 @@ import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.IPDOMVisitor; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorUndefStatement; import org.eclipse.cdt.core.dom.ast.IMacroBinding; @@ -46,7 +45,7 @@ import org.eclipse.core.runtime.IPath; * Represents macro definitions. They are stored with the file and with a PDOMMacroContainer. * The latter also contains the references to all macros with the same name. */ -public class PDOMMacro implements IIndexMacro, IPDOMBinding, IASTFileLocation { +public class PDOMMacro implements IIndexMacro, IPDOMBinding { private static final int CONTAINER = 0; private static final int FILE = 4; private static final int PARAMETERS= 8; @@ -288,7 +287,7 @@ public class PDOMMacro implements IIndexMacro, IPDOMBinding, IASTFileLocation { } @Override - public String getFileName() { + public IASTFileLocation getFileLocation() { try { IIndexFile file = getFile(); if (file == null) { @@ -298,39 +297,17 @@ public class PDOMMacro implements IIndexMacro, IPDOMBinding, IASTFileLocation { // how to implement this. Existing implementations return // the absolute path, so here we attempt to do the same. IPath location = IndexLocationFactory.getAbsolutePath(file.getLocation()); - return location != null ? location.toOSString() : null; + if (location == null) { + return null; + } + String filename = location.toOSString(); + return new PDOMASTFileLocation(filename, getNodeOffset(), getNodeLength()); } catch (CoreException e) { CCorePlugin.log(e); } return null; } - @Override - public int getStartingLineNumber() { - return 0; - } - - @Override - public int getEndingLineNumber() { - return 0; - } - - @Override - public IASTPreprocessorIncludeStatement getContextInclusionStatement() { - return null; - } - - @Override - public IASTFileLocation asFileLocation() { - return this; - } - - @Override - public IASTFileLocation getFileLocation() { - return this; - } - - @Override public int getNodeLength() { try { return fLinkage.getDB().getShort(fRecord + NAME_LENGTH); @@ -340,7 +317,6 @@ public class PDOMMacro implements IIndexMacro, IPDOMBinding, IASTFileLocation { } } - @Override public int getNodeOffset() { try { return fLinkage.getDB().getInt(fRecord + NAME_OFFSET); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroDefinitionName.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroDefinitionName.java index 394ae9c5221..52481198ff5 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroDefinitionName.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroDefinitionName.java @@ -75,7 +75,7 @@ class PDOMMacroDefinitionName implements IIndexFragmentName { @Override public IASTFileLocation getFileLocation() { - return fMacro; + return fMacro.getFileLocation(); } @Override diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroReferenceName.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroReferenceName.java index 489f319a015..e8f25631521 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroReferenceName.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMMacroReferenceName.java @@ -15,7 +15,6 @@ package org.eclipse.cdt.internal.core.pdom.dom; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexName; import org.eclipse.cdt.core.index.IndexLocationFactory; @@ -31,7 +30,7 @@ import org.eclipse.core.runtime.IPath; /** * Represents declarations, definitions and references to bindings, except for macros. */ -public final class PDOMMacroReferenceName implements IIndexFragmentName, IASTFileLocation { +public final class PDOMMacroReferenceName implements IIndexFragmentName { private final PDOMLinkage linkage; private final long record; @@ -206,11 +205,6 @@ public final class PDOMMacroReferenceName implements IIndexFragmentName, IASTFil @Override public IASTFileLocation getFileLocation() { - return this; - } - - @Override - public String getFileName() { try { IIndexFile file = getFile(); if (file == null) { @@ -220,33 +214,17 @@ public final class PDOMMacroReferenceName implements IIndexFragmentName, IASTFil // how to implement this. Existing implementations return // the absolute path, so here we attempt to do the same. IPath location = IndexLocationFactory.getAbsolutePath(file.getLocation()); - return location != null ? location.toOSString() : null; + if (location == null) { + return null; + } + String filename = location.toOSString(); + return new PDOMASTFileLocation(filename, getNodeOffset(), getNodeLength()); } catch (CoreException e) { CCorePlugin.log(e); } return null; } - @Override - public int getStartingLineNumber() { - return 0; - } - - @Override - public int getEndingLineNumber() { - return 0; - } - - @Override - public IASTPreprocessorIncludeStatement getContextInclusionStatement() { - return null; - } - - @Override - public IASTFileLocation asFileLocation() { - return this; - } - @Override public int getNodeLength() { try { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java index d57b6877528..ba7dcb2511f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java @@ -17,7 +17,6 @@ import java.util.ArrayList; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexName; import org.eclipse.cdt.core.index.IndexLocationFactory; @@ -30,7 +29,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; -public final class PDOMName implements IIndexFragmentName, IASTFileLocation { +public final class PDOMName implements IIndexFragmentName { private final PDOMLinkage linkage; private final long record; @@ -332,11 +331,6 @@ public final class PDOMName implements IIndexFragmentName, IASTFileLocation { @Override public IASTFileLocation getFileLocation() { - return this; - } - - @Override - public String getFileName() { try { IIndexFile file = getFile(); if (file == null) { @@ -346,33 +340,17 @@ public final class PDOMName implements IIndexFragmentName, IASTFileLocation { // how to implement this. Existing implementations return // the absolute path, so here we attempt to do the same. IPath location = IndexLocationFactory.getAbsolutePath(file.getLocation()); - return location != null ? location.toOSString() : null; + if (location == null) { + return null; + } + String filename = location.toOSString(); + return new PDOMASTFileLocation(filename, getNodeOffset(), getNodeLength()); } catch (CoreException e) { CCorePlugin.log(e); } return null; } - @Override - public int getStartingLineNumber() { - return 0; - } - - @Override - public int getEndingLineNumber() { - return 0; - } - - @Override - public IASTPreprocessorIncludeStatement getContextInclusionStatement() { - return null; - } - - @Override - public IASTFileLocation asFileLocation() { - return this; - } - @Override public int getNodeLength() { try {