diff --git a/core/org.eclipse.cdt.core.tests/ChangeLog b/core/org.eclipse.cdt.core.tests/ChangeLog index 397512048fc..31381fb7844 100644 --- a/core/org.eclipse.cdt.core.tests/ChangeLog +++ b/core/org.eclipse.cdt.core.tests/ChangeLog @@ -1,3 +1,7 @@ +2003-06-17 Victor Mozgin + Added MacroTests.java (invocation in AllCoreTests). + Added MacroTests.c to resources. + 2003-06-17 Brent Nicolle Added Interface tests of IStructure.java. diff --git a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/MacroTests.java b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/MacroTests.java new file mode 100644 index 00000000000..b449caf25cb --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/MacroTests.java @@ -0,0 +1,113 @@ +/* + * Created on Jun 9, 2003 + * by bnicolle + */ +package org.eclipse.cdt.core.model.tests; + +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.internal.core.model.CElement; + +import junit.framework.*; + +import java.util.Stack; + + +/** + * @author bnicolle + * + */ +public class MacroTests extends IntegratedCModelTest { + /** + * @param name + */ + public MacroTests(String name) { + super(name); + } + + /** + * @see org.eclipse.cdt.internal.core.model.IntegratedCModelTest + */ + public String getSourcefileSubdir() { + return "resources/cmodel/"; + } + + /** + * @see org.eclipse.cdt.internal.core.model.IntegratedCModelTest + */ + public String getSourcefileResource() { + return "MacroTests.c"; + } + + /** + * @returns a test suite named after this class + * containing all its public members named "test*" + */ + public static Test suite() { + TestSuite suite= new TestSuite(MacroTests.class); + return suite; + } + + + /* This is a list of elements in the test .c file. It will be used + * in a number of places in the tests + */ + String[] expectedStringList= {"Z", "X", "Y", + "SomeName", "", "A::BCD", "DEFA", "DB", "B::SomeName", + "PINT", "myPINT", "foobar"}; + int[] expectedOffsets={ 8,26,39,55,75,89,114,130,152,187,212,227}; + int[] expectedLengths={ 1, 1, 1, 1, 1, 8, 4, 2, 18, 4, 6, 6}; + /* This is a list of that the types of the above list of elements is + * expected to be. + */ + int[] expectedTypes= { ICElement.C_MACRO, ICElement.C_MACRO, + ICElement.C_MACRO, ICElement.C_STRUCT, + ICElement.C_STRUCT, ICElement.C_VARIABLE, ICElement.C_MACRO, + ICElement.C_MACRO, ICElement.C_VARIABLE, ICElement.C_MACRO, + ICElement.C_VARIABLE, ICElement.C_FUNCTION_DECLARATION}; + + + public void testMacro_0001() throws CModelException { + ITranslationUnit myTranslationUnit = getTU(); + ICElement myElement; + Stack missing=new Stack(); + int x; + + for (x=0;x= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) | (c == '_')) { + + int baseOffset = lastContext.getOffset() - lastContext.undoStackSize() - 1; if( madeMistake ) madeMistake = false; // String buffer is slow, we need a better way such as memory mapped files @@ -630,7 +632,7 @@ public class Scanner implements IScanner { if (mapping != null) { if( contextStack.shouldExpandDefinition( POUND_DEFINE + ident ) ) { - expandDefinition(ident, mapping); + expandDefinition(ident, mapping, baseOffset); c = getChar(); continue; } @@ -1864,7 +1866,7 @@ public class Scanner implements IScanner { buffer.append((char) c); c = getChar(true); } - + String parameters = buffer.toString(); // replace StringTokenizer later -- not performant @@ -2023,11 +2025,14 @@ public class Scanner implements IScanner { return parameterValues; } - protected void expandDefinition(String symbol, Object expansion) - throws ScannerException { + protected void expandDefinition(String symbol, Object expansion, int symbolOffset) + throws ScannerException + { + // All the tokens generated by the macro expansion + // will have dimensions (offset and length) equal to the expanding symbol. if (expansion instanceof String ) { String replacementValue = (String) expansion; - contextStack.updateContext( new StringReader(replacementValue), (POUND_DEFINE + symbol ), ScannerContext.MACROEXPANSION, null, requestor ); + contextStack.updateContext( new StringReader(replacementValue), (POUND_DEFINE + symbol ), ScannerContext.MACROEXPANSION, null, requestor, symbolOffset, symbol.length()); } else if (expansion instanceof IMacroDescriptor ) { IMacroDescriptor macro = (IMacroDescriptor) expansion; skipOverWhitespace(); @@ -2049,6 +2054,9 @@ public class Scanner implements IScanner { buffer.append((char) c); c = getChar( true ); } + + // Position of the closing ')' + int endMacroOffset = lastContext.getOffset() - lastContext.undoStackSize() - 1; String betweenTheBrackets = buffer.toString().trim(); @@ -2141,10 +2149,10 @@ public class Scanner implements IScanner { if (i < (numberOfTokens-1)) // Do not append to the last one buffer.append( " " ); } - String finalString = buffer.toString(); + String finalString = buffer.toString(); contextStack.updateContext( new StringReader(finalString), - POUND_DEFINE + macro.getSignature(), ScannerContext.MACROEXPANSION, null, requestor ); + POUND_DEFINE + macro.getSignature(), ScannerContext.MACROEXPANSION, null, requestor, symbolOffset, endMacroOffset - symbolOffset + 1 ); } else if (throwExceptionOnBadMacroExpansion) throw new ScannerException( diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ScannerContext.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ScannerContext.java index 1a56bd848ba..2c26bb0a672 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ScannerContext.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ScannerContext.java @@ -21,20 +21,36 @@ public class ScannerContext implements IScannerContext { private Reader reader; private String filename; + private int macroOffset = -1; + private int macroLength = -1; private int offset; private Stack undo = new Stack(); private int kind; public ScannerContext(){} - public IScannerContext initialize(Reader r, String f, int k, IASTInclusion i) + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.IScannerContext#initialize(Reader, String, int, IASTInclusion, int, int) + */ + public IScannerContext initialize(Reader r, String f, int k, IASTInclusion i, int mO, int mL) { reader = r; filename = f; offset = 0; kind = k; - inc = i; + inc = i; + macroOffset = mO; + macroLength = mL; return this; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.IScannerContext#initialize(Reader, String, int, IASTInclusion) + */ + public IScannerContext initialize(Reader r, String f, int k, IASTInclusion i) + { + return initialize(r, f, k, i, -1, -1); + } public int read() throws IOException { ++offset; @@ -49,15 +65,40 @@ public class ScannerContext implements IScannerContext { return filename; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.IScannerContext#getExtension() + */ + public final int getMacroOffset() + { + return macroOffset; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.IScannerContext#getMacroLength() + */ + public final int getMacroLength() + { + return macroLength; + } - /** - * Returns the offset. - * @return int - */ + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.IScannerContext#getOffset() + */ public final int getOffset() { - return offset; + // All the tokens generated by the macro expansion + // will have dimensions (offset and length) equal to the expanding symbol. + return (macroOffset < 0) ? offset : macroOffset; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.IScannerContext#getRelativeOffset() + */ + public final int getRelativeOffset() + { + return offset; + } /** * Returns the reader. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Token.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Token.java index d34eec20235..38415c4c8f2 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Token.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Token.java @@ -20,6 +20,8 @@ public class Token implements IToken { image = i; filename = context.getFilename(); offset = context.getOffset() - image.length() - context.undoStackSize(); + macroOffset = context.getMacroOffset(); + macroLength = context.getMacroLength(); if( type == tLSTRING || type == tSTRING || type == tCHAR ){ offset--; @@ -39,13 +41,18 @@ public class Token implements IToken { public int type; public int getType() { return type; } - public String image; + protected String image; public String getImage() { return image; } public String filename; - public int offset; - public int getOffset() { return offset; } - public int getLength() { return image.length(); } + + protected int offset; + protected int macroOffset = -1; + protected int macroLength = -1; + // All the tokens generated by the macro expansion + // will have dimensions (offset and length) equal to the expanding symbol. + public int getOffset() { return (macroOffset < 0) ? offset : macroOffset; } + public int getLength() { return (macroLength < 0) ? image.length() : macroLength; } public int getEndOffset() { return getOffset() + getLength(); } diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java index 1cbbd467746..397fcfc1792 100644 --- a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java @@ -723,10 +723,10 @@ public class ScannerTestCase extends BaseScannerTest assertNotNull(parms); assertTrue(expansion.size() == 3); assertTrue(((Token) expansion.get(0)).type == IToken.tIDENTIFIER); - assertTrue(((Token) expansion.get(0)).image.equals("x")); + assertTrue(((Token) expansion.get(0)).getImage().equals("x")); assertTrue(((Token) expansion.get(1)).type == IToken.tPLUS); assertTrue(((Token) expansion.get(2)).type == IToken.tINTEGER); - assertTrue(((Token) expansion.get(2)).image.equals("1")); + assertTrue(((Token) expansion.get(2)).getImage().equals("1")); validateIdentifier("y"); validateToken(IToken.tASSIGN);