From af018d72e9b8a4423b900bc5ae6d381fcc8868a2 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Mon, 23 Apr 2007 17:37:31 +0000 Subject: [PATCH] Bug 174034 - Support open declaration on Macros by adding a link to the PDOMFile as well as offset and length info to the PDOMMacro record. The made a sneaky path to get that information to ASTMacroName to support the open on the macro definition. --- .../eclipse/cdt/core/index/IIndexMacro.java | 8 ++ .../core/parser/scanner2/LocationMap.java | 28 ++++-- .../eclipse/cdt/internal/core/pdom/PDOM.java | 3 +- .../cdt/internal/core/pdom/dom/PDOMFile.java | 2 +- .../cdt/internal/core/pdom/dom/PDOMMacro.java | 95 +++++++++++++++++-- 5 files changed, 119 insertions(+), 17 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/index/IIndexMacro.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/index/IIndexMacro.java index fc1f7336468..f208ce2f430 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/index/IIndexMacro.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/index/IIndexMacro.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.core.index; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.parser.IMacro; /** @@ -28,5 +29,12 @@ import org.eclipse.cdt.core.parser.IMacro; * @since 4.0 */ public interface IIndexMacro extends IMacro { + + /** + * If available, return the file location for this macro definition + * otherwise return null + * @return + */ + IASTFileLocation getFileLocation(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java index 9e196ee3434..16098d0b039 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java @@ -43,6 +43,7 @@ import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IMacroBinding; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode; +import org.eclipse.cdt.core.index.IIndexMacro; import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.IMacro; import org.eclipse.cdt.core.parser.util.ArrayUtil; @@ -813,14 +814,19 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @author jcamelon */ public class ASTMacroName extends ASTNode implements IASTName { - private final char[] name; - private IMacroBinding binding = null; - - + private final char[] name; + private final IASTFileLocation fileLocation; + private IMacroBinding binding = null; + public ASTMacroName(char[] n) { - this.name = n; + this(n, null); } + public ASTMacroName(char [] n, IASTFileLocation fileLocation) { + this.name = n; + this.fileLocation = fileLocation; + } + /* * (non-Javadoc) * @@ -901,6 +907,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { //do nothing } + public IASTFileLocation getFileLocation() { + return fileLocation != null ? fileLocation : super.getFileLocation(); + } + public ILinkage getLinkage() { return Linkage.NO_LINKAGE; } @@ -1464,6 +1474,12 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { return expansion; } + public IASTFileLocation getFileLocation() { + return macro != null && macro instanceof IIndexMacro + ? ((IIndexMacro)macro).getFileLocation() + : null; + } + public IMacroBinding getBinding() { return bind; } @@ -1683,7 +1699,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { r = new ASTObjectMacro(); } - IASTName name = new ASTMacroName(d.name); + IASTName name = new ASTMacroName(d.name, d.getFileLocation()); name.setPropertyInParent(IASTPreprocessorMacroDefinition.MACRO_NAME); name.setParent(r); ((ASTNode) name).setOffsetAndLength(d.nameOffset, d.name.length); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java index f564cfbf506..4c93eff112b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java @@ -75,7 +75,7 @@ import org.eclipse.core.runtime.Status; public class PDOM extends PlatformObject implements IIndexFragment, IPDOM { protected Database db; - public static final int VERSION = 30; + public static final int VERSION = 31; // 0 - the beginning of it all // 1 - first change to kick off upgrades // 2 - added file inclusions @@ -107,6 +107,7 @@ public class PDOM extends PlatformObject implements IIndexFragment, IPDOM { // 28 - templates: class instance/specialization base classes // 29 - includes: fixed modelling of unresolved includes (180159) // 30 - templates: method/constructor templates, typedef specializations + // 31 - macros: added file locations public static final int LINKAGES = Database.DATA_AREA; public static final int FILE_INDEX = Database.DATA_AREA + 4; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java index 835a328299e..2c052a8046b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java @@ -176,7 +176,7 @@ public class PDOMFile implements IIndexFragmentFile { PDOMMacro lastMacro= null; for (int i = 0; i < macros.length; i++) { IASTPreprocessorMacroDefinition macro = macros[i]; - PDOMMacro pdomMacro = new PDOMMacro(pdom, macro); + PDOMMacro pdomMacro = new PDOMMacro(pdom, macro, this); if (lastMacro == null) { setFirstMacro(pdomMacro); } 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 853fbf9613c..4dae36f2345 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 @@ -12,13 +12,17 @@ package org.eclipse.cdt.internal.core.pdom.dom; +import java.net.URI; import java.util.ArrayList; import java.util.List; import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFunctionStyleMacroParameter; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; +import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexMacro; import org.eclipse.cdt.core.parser.IMacro; import org.eclipse.cdt.internal.core.parser.scanner2.FunctionStyleMacro; @@ -33,30 +37,38 @@ import org.eclipse.core.runtime.CoreException; * * @author Doug Schaefer */ -public class PDOMMacro implements IIndexMacro { +public class PDOMMacro implements IIndexMacro, IASTFileLocation { private final PDOM pdom; private final int record; private IMacro macro; private static final int NAME = 0; - private static final int FIRST_PARAMETER = 4; - private static final int EXPANSION = 8; - private static final int NEXT_MACRO = 12; + private static final int FILE = 4; + private static final int NAME_OFFSET = 8; + private static final int NAME_LENGTH = 12; // short + private static final int FIRST_PARAMETER = 14; + private static final int EXPANSION = 18; + private static final int NEXT_MACRO = 22; - private static final int RECORD_SIZE = 16; + private static final int RECORD_SIZE = 26; public PDOMMacro(PDOM pdom, int record) { this.pdom = pdom; this.record = record; } - public PDOMMacro(PDOM pdom, IASTPreprocessorMacroDefinition macro) throws CoreException { + public PDOMMacro(PDOM pdom, IASTPreprocessorMacroDefinition macro, PDOMFile file) throws CoreException { this.pdom = pdom; Database db = pdom.getDB(); this.record = db.malloc(RECORD_SIZE); - db.putInt(record + NAME, db.newString(macro.getName().toCharArray()).getRecord()); + IASTName name = macro.getName(); + db.putInt(record + NAME, db.newString(name.toCharArray()).getRecord()); + db.putInt(record + FILE, file.getRecord()); + IASTFileLocation fileloc = name.getFileLocation(); + db.putInt(record + NAME_OFFSET, fileloc.getNodeOffset()); + db.putShort(record + NAME_LENGTH, (short) fileloc.getNodeLength()); db.putInt(record + EXPANSION, db.newString(macro.getExpansion()).getRecord()); setNextMacro(0); @@ -118,22 +130,28 @@ public class PDOMMacro implements IIndexMacro { return rec != 0 ? new PDOMMacroParameter(pdom, rec) : null; } - private class ObjectStylePDOMMacro extends ObjectStyleMacro { + private class ObjectStylePDOMMacro extends ObjectStyleMacro implements IIndexMacro { public ObjectStylePDOMMacro(char[] name) { super(name, null); } public char[] getExpansion() { return getMacroExpansion(); } + public IASTFileLocation getFileLocation() { + return PDOMMacro.this; + } } - private class FunctionStylePDOMMacro extends FunctionStyleMacro { + private class FunctionStylePDOMMacro extends FunctionStyleMacro implements IIndexMacro { public FunctionStylePDOMMacro(char[] name, char[][] arglist) { super(name, null, arglist); } public char[] getExpansion() { return getMacroExpansion(); } + public IASTFileLocation getFileLocation() { + return PDOMMacro.this; + } } private char[] getMacroExpansion() { @@ -194,4 +212,63 @@ public class PDOMMacro implements IIndexMacro { } return macro.getName(); } + + public IIndexFile getFile() throws CoreException { + int filerec = pdom.getDB().getInt(record + FILE); + return filerec != 0 ? new PDOMFile(pdom, filerec) : null; + } + + public int getEndingLineNumber() { + return 0; + } + + public String getFileName() { + try { + PDOMFile file = (PDOMFile) getFile(); + if(file!=null) { + /* + * We need to spec. what this method can return to know + * how to implement this. Existing implmentations return + * the absolute path, so here we attempt to do the same. + */ + URI uri = file.getLocation().getURI(); + if ("file".equals(uri.getScheme())) //$NON-NLS-1$ + return uri.getSchemeSpecificPart(); + } + } catch (CoreException e) { + CCorePlugin.log(e); + } + return null; + } + + public int getStartingLineNumber() { + return 0; + } + + public IASTFileLocation asFileLocation() { + return this; + } + + public IASTFileLocation getFileLocation() { + return this; + } + + public int getNodeLength() { + try { + return pdom.getDB().getShort(record + NAME_LENGTH); + } catch (CoreException e) { + CCorePlugin.log(e); + return 0; + } + } + + public int getNodeOffset() { + try { + return pdom.getDB().getInt(record + NAME_OFFSET); + } catch (CoreException e) { + CCorePlugin.log(e); + return 0; + } + } + }