1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-09 09:15:38 +02:00

Added function style macros. Redid how macros are handled in the fast indexer. And a few minor bug fixes in the Actions.

This commit is contained in:
Doug Schaefer 2006-05-10 19:25:09 +00:00
parent 4a428df9d4
commit be3041c07d
15 changed files with 192 additions and 93 deletions

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.core.dom;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.core.parser.IScanner;
/**
* This is the interface that an AST Service uses to delegate the construction
@ -42,7 +43,7 @@ public interface ICodeReaderFactory {
* @param path
* @return CodeReader for contents at that path.
*/
public CodeReader createCodeReaderForInclusion(String path);
public CodeReader createCodeReaderForInclusion(IScanner scanner, String path);
/**
* Returns the ICodeReaderCache used for this ICodeReaderFacotry.

View file

@ -29,7 +29,9 @@ public interface IScanner {
public void setContentAssistMode( int offset );
public void setASTFactory( IASTFactory f );
public void addDefinition(char[] key, char[] value);
public IMacro addDefinition(char[] key, char[] value);
public IMacro addDefinition(char[] name, char[][] params, char[] expansion);
public Map getDefinitions();
public String[] getIncludePaths();

View file

@ -1410,17 +1410,13 @@ abstract class BaseScanner implements IScanner {
pushContext(r.buffer, d);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.parser.IScanner#addDefinition(java.lang.String,
* java.lang.String)
*/
public void addDefinition(char[] key, char[] value) {
public IMacro addDefinition(char[] key, char[] value) {
int idx = CharArrayUtils.indexOf('(', key);
if (idx == -1)
definitions.put(key, new ObjectStyleMacro(key, value));
else {
if (idx == -1) {
IMacro macro = new ObjectStyleMacro(key, value);
definitions.put(key, macro);
return macro;
} else {
pushContext(key);
bufferPos[bufferStackPos] = idx;
char[][] args = null;
@ -1432,25 +1428,22 @@ abstract class BaseScanner implements IScanner {
if (args != null) {
key = CharArrayUtils.extract(key, 0, idx);
definitions.put(key, new FunctionStyleMacro(key, value, args));
}
return addDefinition(key, args, value);
} else
return null;
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.parser.IScanner#getCount()
*/
public IMacro addDefinition(char[] name, char[][] params, char[] expansion) {
IMacro macro = new FunctionStyleMacro(name, expansion, params);
definitions.put(name, macro);
return macro;
}
public int getCount() {
return count;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.parser.IScanner#getDefinitions()
*/
public Map getDefinitions() {
CharArrayObjectMap objMap = getRealDefinitions();
int size = objMap.size();

View file

@ -76,40 +76,45 @@ public class DOMScanner extends BaseScanner {
private void registerMacros() {
for( int i = 0; i < definitions.size(); ++i )
{
IMacro m = (IMacro) definitions.get( definitions.keyAt(i) );
if( m instanceof ObjectStyleMacro && ((ObjectStyleMacro)m).attachment != null )
continue;
if( m instanceof DynamicStyleMacro )
{
DynamicStyleMacro macro = (DynamicStyleMacro) m;
macro.attachment = locationMap.registerBuiltinDynamicStyleMacro( macro );
}
else if( m instanceof DynamicFunctionStyleMacro )
{
DynamicFunctionStyleMacro macro = (DynamicFunctionStyleMacro) m;
macro.attachment = locationMap.registerBuiltinDynamicFunctionStyleMacro( macro );
}
else if( m instanceof FunctionStyleMacro )
{
FunctionStyleMacro macro = (FunctionStyleMacro) m;
macro.attachment = locationMap.registerBuiltinFunctionStyleMacro( macro );
}
else if( m instanceof ObjectStyleMacro )
{
ObjectStyleMacro macro = (ObjectStyleMacro) m;
macro.attachment = locationMap.registerBuiltinObjectStyleMacro( macro );
}
registerMacro((IMacro)definitions.get(definitions.keyAt(i)));
}
}
private void registerMacro(IMacro m) {
if (m == null)
return;
if (m instanceof ObjectStyleMacro && ((ObjectStyleMacro)m).attachment != null)
return;
if (m instanceof DynamicStyleMacro) {
DynamicStyleMacro macro = (DynamicStyleMacro) m;
macro.attachment = locationMap.registerBuiltinDynamicStyleMacro( macro );
} else if (m instanceof DynamicFunctionStyleMacro) {
DynamicFunctionStyleMacro macro = (DynamicFunctionStyleMacro) m;
macro.attachment = locationMap.registerBuiltinDynamicFunctionStyleMacro( macro );
} else if (m instanceof FunctionStyleMacro) {
FunctionStyleMacro macro = (FunctionStyleMacro) m;
macro.attachment = locationMap.registerBuiltinFunctionStyleMacro( macro );
} else if (m instanceof ObjectStyleMacro) {
ObjectStyleMacro macro = (ObjectStyleMacro) m;
macro.attachment = locationMap.registerBuiltinObjectStyleMacro( macro );
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.parser.IScanner#getLocationResolver()
*/
public IMacro addDefinition(char[] key, char[] value) {
IMacro macro = super.addDefinition(key, value);
if (locationMap != null)
registerMacro(macro);
return macro;
}
public IMacro addDefinition(char[] name, char[][] params, char[] expansion) {
IMacro macro = super.addDefinition(name, params, expansion);
if (locationMap != null)
registerMacro(macro);
return macro;
}
public ILocationResolver getLocationResolver() {
if (locationMap instanceof ILocationResolver)
return (ILocationResolver) locationMap;
@ -170,7 +175,7 @@ public class DOMScanner extends BaseScanner {
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createReaderDuple(java.lang.String)
*/
protected CodeReader createReaderDuple(String finalPath) {
return codeReaderFactory.createCodeReaderForInclusion(finalPath);
return codeReaderFactory.createCodeReaderForInclusion(this, finalPath);
}
/*

View file

@ -14,6 +14,7 @@ import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.internal.core.dom.parser.EmptyCodeReaderCache;
/**
@ -50,7 +51,7 @@ public class FileCodeReaderFactory implements ICodeReaderFactory {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/
public CodeReader createCodeReaderForInclusion(String path) {
public CodeReader createCodeReaderForInclusion(IScanner scanner, String path) {
return cache.get(path);
}

View file

@ -68,13 +68,14 @@ public class PDOM extends PlatformObject
private final IPath dbPath;
private Database db;
public static final int VERSION = 5;
public static final int VERSION = 6;
// 0 - the beginning of it all
// 1 - first change to kick off upgrades
// 2 - added file inclusions
// 3 - added macros and change string implementation
// 4 - added parameters in C++
// 5 - added types and restructured nodes a bit
// 6 - function style macros.
public static final int LINKAGES = Database.DATA_AREA;
public static final int FILE_INDEX = Database.DATA_AREA + 4;

View file

@ -23,11 +23,13 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.internal.core.pdom.db.IString;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMFile;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMInclude;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMMacro;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMMacroParameter;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
@ -39,8 +41,10 @@ import org.eclipse.core.runtime.Status;
public class PDOMCodeReaderFactory implements ICodeReaderFactory {
private final PDOM pdom;
private List workingCopies = new ArrayList(1);
private Set skippedHeaders = new HashSet();
private static final char[] EMPTY_CHARS = new char[0];
public PDOMCodeReaderFactory(PDOM pdom) {
this.pdom = pdom;
@ -55,10 +59,6 @@ public class PDOMCodeReaderFactory implements ICodeReaderFactory {
return 0;
}
public Set getSkippedHeaders() {
return skippedHeaders;
}
public CodeReader createCodeReaderForTranslationUnit(String path) {
return ParserUtil.createReader(path,
workingCopies != null ? workingCopies.iterator() : null);
@ -68,7 +68,7 @@ public class PDOMCodeReaderFactory implements ICodeReaderFactory {
return new CodeReader(tu.getResource().getLocation().toOSString(), tu.getContents());
}
private void fillMacros(PDOMFile file, StringBuffer buffer, Set visited) throws CoreException {
private void fillMacros(PDOMFile file, IScanner scanner, Set visited) throws CoreException {
IString filename = file.getFileName();
if (visited.contains(filename))
return;
@ -77,23 +77,32 @@ public class PDOMCodeReaderFactory implements ICodeReaderFactory {
// Follow the includes
PDOMInclude include = file.getFirstInclude();
while (include != null) {
fillMacros(include.getIncludes(), buffer, visited);
fillMacros(include.getIncludes(), scanner, visited);
include = include.getNextInIncludes();
}
// Add in my macros now
PDOMMacro macro = file.getFirstMacro();
while (macro != null) {
buffer.append("#define "); //$NON-NLS-1$
buffer.append(macro.getName().getChars());
buffer.append(' ');
buffer.append(macro.getExpansion().getChars());
buffer.append('\n');
char[] name = macro.getName().getChars();
char[] expansion = macro.getExpansion().getChars();
PDOMMacroParameter param = macro.getFirstParameter();
if (param != null) {
List paramList = new ArrayList();
while (param != null) {
paramList.add(param.getName().getChars());
param = param.getNextParameter();
}
char[][] params = (char[][])paramList.toArray(new char[paramList.size()][]);
scanner.addDefinition(name, params, expansion);
} else
scanner.addDefinition(name, expansion);
macro = macro.getNextMacro();
}
}
public CodeReader createCodeReaderForInclusion(String path) {
public CodeReader createCodeReaderForInclusion(IScanner scanner, String path) {
// Don't parse inclusion if it is already captured
try {
try {
@ -106,15 +115,10 @@ public class PDOMCodeReaderFactory implements ICodeReaderFactory {
}
PDOMFile file = pdom.getFile(path);
if (file != null) {
// Already got things from here, pass in a magic
// buffer with the macros in it
skippedHeaders.add(path);
StringBuffer buffer = new StringBuffer();
fillMacros(file, buffer, new HashSet());
int length = buffer.length();
char[] chars = new char[length];
buffer.getChars(0, length, chars, 0);
return new CodeReader(path, chars);
// Already got things from here,
// add the macros to the scanner
fillMacros(file, scanner, new HashSet());
return new CodeReader(path, EMPTY_CHARS);
}
} catch (CoreException e) {
CCorePlugin.log(new CoreException(new Status(IStatus.ERROR,

View file

@ -11,6 +11,8 @@
package org.eclipse.cdt.internal.core.pdom.dom;
import org.eclipse.cdt.core.dom.ast.IASTFunctionStyleMacroParameter;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.Database;
@ -28,10 +30,11 @@ public class PDOMMacro {
private final int record;
private static final int NAME = 0;
private static final int EXPANSION = 4;
private static final int NEXT_MACRO = 8;
private static final int FIRST_PARAMETER = 4;
private static final int EXPANSION = 8;
private static final int NEXT_MACRO = 12;
private static final int RECORD_SIZE = 12;
private static final int RECORD_SIZE = 16;
public PDOMMacro(PDOM pdom, int record) {
this.pdom = pdom;
@ -46,6 +49,20 @@ public class PDOMMacro {
db.putInt(record + NAME, db.newString(macro.getName().toCharArray()).getRecord());
db.putInt(record + EXPANSION, db.newString(macro.getExpansion()).getRecord());
setNextMacro(0);
PDOMMacroParameter last = null;
if (macro instanceof IASTPreprocessorFunctionStyleMacroDefinition) {
IASTPreprocessorFunctionStyleMacroDefinition func = (IASTPreprocessorFunctionStyleMacroDefinition)macro;
IASTFunctionStyleMacroParameter[] params = func.getParameters();
for (int i = params.length - 1; i >= 0; --i) {
IASTFunctionStyleMacroParameter param = params[i];
PDOMMacroParameter pdomParam = new PDOMMacroParameter(pdom, param.getParameter());
if (last != null)
pdomParam.setNextParameter(last);
last = pdomParam;
}
}
db.putInt(record + FIRST_PARAMETER, last != null ? last.getRecord() : 0);
}
public int getRecord() {
@ -80,4 +97,10 @@ public class PDOMMacro {
public void setNextMacro(int rec) throws CoreException {
pdom.getDB().putInt(record + NEXT_MACRO, rec);
}
public PDOMMacroParameter getFirstParameter() throws CoreException {
int rec = pdom.getDB().getInt(record + FIRST_PARAMETER);
return rec != 0 ? new PDOMMacroParameter(pdom, rec) : null;
}
}

View file

@ -0,0 +1,67 @@
/*******************************************************************************
* Copyright (c) 2006 QNX Software Systems and others.
* 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
*
* Contributors:
* QNX - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.cdt.internal.core.pdom.db.IString;
import org.eclipse.core.runtime.CoreException;
/**
* @author Doug Schaefer
*
*/
public class PDOMMacroParameter {
private final PDOM pdom;
private final int record;
private static final int NEXT = 0;
private static final int NAME = 4;
private static final int RECORD_SIZE = 8;
public PDOMMacroParameter(PDOM pdom, int record) {
this.pdom = pdom;
this.record = record;
}
public PDOMMacroParameter(PDOM pdom, String name) throws CoreException {
Database db = pdom.getDB();
this.pdom = pdom;
this.record = db.malloc(RECORD_SIZE);
db.putInt(record + NEXT, 0);
db.putInt(record + NAME, db.newString(name).getRecord());
}
public int getRecord() {
return record;
}
public void setNextParameter(PDOMMacroParameter next) throws CoreException {
int rec = next != null ? next.getRecord() : 0;
pdom.getDB().putInt(record + NEXT, rec);
}
public PDOMMacroParameter getNextParameter() throws CoreException {
int rec = pdom.getDB().getInt(record + NEXT);
return rec != 0 ? new PDOMMacroParameter(pdom, rec) : null;
}
public IString getName() throws CoreException {
Database db = pdom.getDB();
return db.getString(db.getInt(record + NAME));
}
}

View file

@ -149,7 +149,7 @@ class PDOMFastHandleDelta extends PDOMFastIndexerJob {
file.clear();
// Add the new symbols
addSymbols(tu.getLanguage(), ast, codeReaderFactory.getSkippedHeaders());
addSymbols(tu.getLanguage(), ast);
} finally {
pdom.releaseWriteLock();
}

View file

@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.core.pdom.indexer.fast;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
@ -71,7 +70,7 @@ public abstract class PDOMFastIndexerJob extends Job {
pdom.acquireWriteLock();
try {
addSymbols(language, ast, codeReaderFactory.getSkippedHeaders());
addSymbols(language, ast);
} finally {
pdom.releaseWriteLock();
}
@ -80,7 +79,7 @@ public abstract class PDOMFastIndexerJob extends Job {
pdom.fireChange();
}
protected void addSymbols(ILanguage language, IASTTranslationUnit ast, Set skippedHeaders) throws InterruptedException, CoreException {
protected void addSymbols(ILanguage language, IASTTranslationUnit ast) throws InterruptedException, CoreException {
final PDOMLinkage linkage = pdom.getLinkage(language);
if (linkage == null)
return;
@ -112,9 +111,6 @@ public abstract class PDOMFastIndexerJob extends Job {
continue; // skip built-ins and command line macros
String filename = sourceLoc.getFileName();
if (skippedHeaders.contains(filename))
continue;
PDOMFile sourceFile = getCachedFile(filename);
sourceFile.addMacro(macro);
}

View file

@ -12,12 +12,14 @@ package org.eclipse.cdt.internal.core.dom;
import java.util.Arrays;
import java.util.Iterator;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopyProvider;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator;
@ -70,7 +72,7 @@ public class PartialWorkingCopyCodeReaderFactory
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/
public CodeReader createCodeReaderForInclusion(String path) {
public CodeReader createCodeReaderForInclusion(IScanner scanner, String path) {
return cache.get( path );
}

View file

@ -17,6 +17,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.CodeReaderCache;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.core.runtime.Preferences;
/**
@ -85,7 +86,7 @@ public class SavedCodeReaderFactory implements ICodeReaderFactory {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/
public CodeReader createCodeReaderForInclusion(String path) {
public CodeReader createCodeReaderForInclusion(IScanner scanner, String path) {
return cache.get(path);
}

View file

@ -45,7 +45,7 @@ public class FindDeclarationsAction extends IndexAction {
ICProject project = binding.getPDOM().getProject();
PDOMSearchBindingQuery query = new PDOMSearchBindingQuery(
new ICElement[] { project },
getBinding(),
binding,
PDOMSearchBindingQuery.FIND_DECLARATIONS | PDOMSearchBindingQuery.FIND_DEFINITIONS);
NewSearchUI.activateSearchResultView();

View file

@ -555,6 +555,9 @@ public class SelectionParseAction extends Action {
*/
protected void open(IASTName name) throws CoreException {
IASTFileLocation fileloc = name.getFileLocation();
if (fileloc == null)
// no source location - TODO spit out an error in the status bar
return;
int currentOffset = fileloc.getNodeOffset();
int currentLength = fileloc.getNodeLength();