diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ExpansionExplorerTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ExpansionExplorerTests.java
index 5d9868ee808..7c103432df6 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ExpansionExplorerTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ExpansionExplorerTests.java
@@ -45,6 +45,8 @@ public class ExpansionExplorerTests extends BaseTestCase {
}
final MacroExpander expander= createExpander(input[0]);
final String original= input[1];
+
+ verifyStepCount(expander, original, steps);
verifyStep(expander, original, Integer.MAX_VALUE, original, input[steps+1]);
for (i= 0; i < steps; i++) {
@@ -52,18 +54,24 @@ public class ExpansionExplorerTests extends BaseTestCase {
}
}
+ private void verifyStepCount(MacroExpander expander, String original, int steps) {
+ MacroExpansionTracker tracker= new MacroExpansionTracker(Integer.MAX_VALUE);
+ expander.expand(original, tracker, "", 1);
+ assertEquals(steps, tracker.getStepCount());
+ }
+
private void verifyStep(MacroExpander expander, String original, int step, String expectedPre,
String expectedPost) {
MacroExpansionTracker tracker= new MacroExpansionTracker(step);
- expander.expand(original, tracker);
+ expander.expand(original, tracker, "", 1);
String pre = tracker.getCodeBeforeStep();
ReplaceEdit replacement = tracker.getReplacement();
assertNotNull(pre);
assertNotNull(replacement);
String post= apply(pre, replacement);
- assertEquals(expectedPre, pre);
- assertEquals(expectedPost, post);
+ assertEquals("incorrect value pre " + step, expectedPre, pre);
+ assertEquals("incorrect value post " + step, expectedPost, post);
}
private String apply(String pre, ReplaceEdit replacement) {
@@ -91,7 +99,7 @@ public class ExpansionExplorerTests extends BaseTestCase {
// B
public void testNoOp() throws Exception {
- performTest(1);
+ performTest(0);
}
// #define A B
@@ -208,6 +216,69 @@ public class ExpansionExplorerTests extends BaseTestCase {
performTest(7);
}
+ // #define id(x) x
+
+ // id(
+ // id(a))
+
+ // id(
+ // a)
+
+ // a
+ public void testNewline() throws Exception {
+ performTest(2);
+ }
+
+ // #define f x _a _b x
+ // #define _a a
+ // #define _b b
+
+ // f
+
+ // x _a _b x
+
+ // x a _b x
+
+ // x a b x
+ public void testSpace() throws Exception {
+ performTest(3);
+ }
+
+ // #define L __LINE__
+ // #define x(a) a
+
+ // x(L)
+ // x(__LINE__)
+
+ // x(1)
+
+ // 1
+ public void testLineNumber() throws Exception {
+ performTest(3);
+ }
+
+ // #define L __LINE__
+ // #define x(a,b) a,b
+
+ // x(L,
+ // L)
+
+ // x(__LINE__,
+ // L)
+
+ // x(2,
+ // L)
+
+ // x(2,
+ // __LINE__)
+
+ // x(2,
+ // 2)
+
+ // 2,2
+ public void testLineNumber2() throws Exception {
+ performTest(5);
+ }
}
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTests.java
index 6b9249cdd4b..52306c9d717 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTests.java
@@ -639,6 +639,19 @@ public class PreprocessorTests extends PreprocessorTestsBase {
validateProblemCount(0);
}
+
+ // #define A(x,y,z) x + y + z
+ // #define _t t
+ // A ( _t , , _t )
+ public void testEmptyToken() throws Exception {
+ initializeScanner();
+ validateIdentifier("t");
+ validateToken(IToken.tPLUS);
+ validateToken(IToken.tPLUS);
+ validateIdentifier("t");
+ }
+
+
// #define FOO 5
// # define BAR 10
// int x = FOO + BAR;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorMacroDefinition.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorMacroDefinition.java
index b9a97e535e5..00999ca6bc2 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorMacroDefinition.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorMacroDefinition.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
+ * Copyright (c) 2004, 2008 IBM Corporation 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
@@ -41,9 +41,7 @@ public interface IASTPreprocessorMacroDefinition extends
public void setName(IASTName name);
/**
- * Get the macro expansion.
- *
- * @return String
+ * Returns the macro expansion, or an empty string for dynamic style macros.
*/
public String getExpansion();
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IMacroBinding.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IMacroBinding.java
index 9665faa8631..bdc15b5d10e 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IMacroBinding.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IMacroBinding.java
@@ -1,12 +1,13 @@
/*******************************************************************************
- * Copyright (c) 2004, 2005 IBM Corporation and others.
+ * Copyright (c) 2004, 2008 IBM Corporation 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:
- * IBM - Initial API and implementation
+ * IBM - Initial API and implementation
+ * Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.core.dom.ast;
@@ -29,7 +30,7 @@ public interface IMacroBinding extends IBinding {
char[][] getParameterList();
/**
- * Returns the expansion of this macro definition.
+ * Returns the expansion of this macro definition, or null
for dynamic-style macros.
* @since 5.0
*/
char[] getExpansion();
@@ -44,7 +45,7 @@ public interface IMacroBinding extends IBinding {
char[][] getParameterPlaceholderList();
/**
- * Returns the image of the expansion (also containing comments).
+ * Returns the image of the expansion (also containing comments), or null
for dynamic style macros.
* @since 5.0
*/
char[] getExpansionImage();
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ASTPreprocessorNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ASTPreprocessorNode.java
index 4e54f2961dc..b0187448c3a 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ASTPreprocessorNode.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ASTPreprocessorNode.java
@@ -230,7 +230,7 @@ class ASTInclusionStatement extends ASTPreprocessorNode implements IASTPreproces
}
}
-class ASTObjectStyleMacroDefinition extends ASTPreprocessorNode implements IASTPreprocessorObjectStyleMacroDefinition {
+class ASTMacroDefinition extends ASTPreprocessorNode implements IASTPreprocessorObjectStyleMacroDefinition {
private final ASTPreprocessorName fName;
private final int fExpansionNumber;
private final int fExpansionOffset;
@@ -238,7 +238,7 @@ class ASTObjectStyleMacroDefinition extends ASTPreprocessorNode implements IASTP
/**
* Regular constructor.
*/
- public ASTObjectStyleMacroDefinition(IASTTranslationUnit parent, IMacroBinding macro,
+ public ASTMacroDefinition(IASTTranslationUnit parent, IMacroBinding macro,
int startNumber, int nameNumber, int nameEndNumber, int expansionNumber, int endNumber) {
super(parent, IASTTranslationUnit.PREPROCESSOR_STATEMENT, startNumber, endNumber);
fExpansionNumber= expansionNumber;
@@ -250,7 +250,7 @@ class ASTObjectStyleMacroDefinition extends ASTPreprocessorNode implements IASTP
* Constructor for built-in macros
* @param expansionOffset
*/
- public ASTObjectStyleMacroDefinition(IASTTranslationUnit parent, IMacroBinding macro, IASTFileLocation floc, int expansionOffset) {
+ public ASTMacroDefinition(IASTTranslationUnit parent, IMacroBinding macro, IASTFileLocation floc, int expansionOffset) {
super(parent, IASTTranslationUnit.PREPROCESSOR_STATEMENT, -1, -1);
fName= new ASTBuiltinName(this, IASTPreprocessorMacroDefinition.MACRO_NAME, floc, macro.getNameCharArray(), macro);
fExpansionNumber= -1;
@@ -270,7 +270,12 @@ class ASTObjectStyleMacroDefinition extends ASTPreprocessorNode implements IASTP
}
public String getExpansion() {
- return new String(getMacro().getExpansion());
+ final char[] expansion = getMacro().getExpansion();
+ // for dynamic style macros return an empty string
+ if (expansion == null) {
+ return ""; //$NON-NLS-1$
+ }
+ return new String(expansion);
}
public IASTName getName() {
@@ -306,7 +311,10 @@ class ASTObjectStyleMacroDefinition extends ASTPreprocessorNode implements IASTP
if (fExpansionOffset >= 0) {
String fileName= fName.getContainingFilename();
if (fileName != null) {
- return new ASTFileLocationForBuiltins(fileName, fExpansionOffset, getMacro().getExpansionImage().length);
+ final char[] expansionImage = getMacro().getExpansionImage();
+ if (expansionImage != null) {
+ return new ASTFileLocationForBuiltins(fileName, fExpansionOffset, expansionImage.length);
+ }
}
}
return null;
@@ -328,7 +336,7 @@ class ASTMacroParameter extends ASTPreprocessorNode implements IASTFunctionStyle
public void setParameter(String value) {assert false;}
}
-class ASTFunctionStyleMacroDefinition extends ASTObjectStyleMacroDefinition implements IASTPreprocessorFunctionStyleMacroDefinition {
+class ASTFunctionStyleMacroDefinition extends ASTMacroDefinition implements IASTPreprocessorFunctionStyleMacroDefinition {
/**
* Regular constructor.
*/
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java
index 74dc2ff4a4f..85c9ab6d0f6 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java
@@ -14,8 +14,6 @@ package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.File;
import java.io.IOException;
-import java.text.DateFormatSymbols;
-import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -66,8 +64,6 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
public static final int tNOSPACE= IToken.FIRST_RESERVED_PREPROCESSOR+4;
public static final int tMACRO_PARAMETER= IToken.FIRST_RESERVED_PREPROCESSOR+5;
-
-
private static final int ORIGIN_PREPROCESSOR_DIRECTIVE = OffsetLimitReachedException.ORIGIN_PREPROCESSOR_DIRECTIVE;
private static final int ORIGIN_INACTIVE_CODE = OffsetLimitReachedException.ORIGIN_INACTIVE_CODE;
// private static final int ORIGIN_MACRO_EXPANSION = OffsetLimitReachedException.ORIGIN_MACRO_EXPANSION;
@@ -83,6 +79,10 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
private static final ObjectStyleMacro __STDC_HOSTED__ = new ObjectStyleMacro("__STDC_HOSTED_".toCharArray(), ONE); //$NON-NLS-1$
private static final ObjectStyleMacro __STDC_VERSION__ = new ObjectStyleMacro("__STDC_VERSION_".toCharArray(), "199901L".toCharArray()); //$NON-NLS-1$ //$NON-NLS-2$
+ private static final DynamicStyleMacro __FILE__= new FileMacro("__FILE__".toCharArray()); //$NON-NLS-1$
+ private static final DynamicStyleMacro __DATE__= new DateMacro("__DATE__".toCharArray()); //$NON-NLS-1$
+ private static final DynamicStyleMacro __TIME__ = new TimeMacro("__TIME__".toCharArray()); //$NON-NLS-1$
+ private static final DynamicStyleMacro __LINE__ = new LineMacro("__LINE__".toCharArray()); //$NON-NLS-1$
private interface IIncludeFileTester {
Object checkFile(String path, String fileName);
@@ -104,63 +104,6 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
}
};
- // standard built-ins
- final private DynamicStyleMacro __FILE__= new DynamicStyleMacro("__FILE__".toCharArray()) { //$NON-NLS-1$
- public Token execute() {
- StringBuffer buffer = new StringBuffer("\""); //$NON-NLS-1$
- buffer.append(getCurrentFilename());
- buffer.append('\"');
- return new TokenWithImage(IToken.tSTRING, null, 0, 0, buffer.toString().toCharArray());
- }
- };
- final private DynamicStyleMacro __DATE__= new DynamicStyleMacro("__DATE__".toCharArray()) { //$NON-NLS-1$
- final private void append(StringBuffer buffer, int value) {
- if (value < 10)
- buffer.append("0"); //$NON-NLS-1$
- buffer.append(value);
- }
-
- public Token execute() {
- StringBuffer buffer = new StringBuffer("\""); //$NON-NLS-1$
- Calendar cal = Calendar.getInstance();
- DateFormatSymbols dfs= new DateFormatSymbols();
- buffer.append(dfs.getShortMonths()[cal.get(Calendar.MONTH)]);
- buffer.append(" "); //$NON-NLS-1$
- append(buffer, cal.get(Calendar.DAY_OF_MONTH));
- buffer.append(" "); //$NON-NLS-1$
- buffer.append(cal.get(Calendar.YEAR));
- buffer.append("\""); //$NON-NLS-1$
- return new TokenWithImage(IToken.tSTRING, null, 0, 0, buffer.toString().toCharArray());
- }
- };
-
- final private DynamicStyleMacro __TIME__ = new DynamicStyleMacro("__TIME__".toCharArray()) { //$NON-NLS-1$
- final private void append(StringBuffer buffer, int value) {
- if (value < 10)
- buffer.append("0"); //$NON-NLS-1$
- buffer.append(value);
- }
-
- public Token execute() {
- StringBuffer buffer = new StringBuffer("\""); //$NON-NLS-1$
- Calendar cal = Calendar.getInstance();
- append(buffer, cal.get(Calendar.HOUR_OF_DAY));
- buffer.append(":"); //$NON-NLS-1$
- append(buffer, cal.get(Calendar.MINUTE));
- buffer.append(":"); //$NON-NLS-1$
- append(buffer, cal.get(Calendar.SECOND));
- buffer.append("\""); //$NON-NLS-1$
- return new TokenWithImage(IToken.tSTRING, null, 0, 0, buffer.toString().toCharArray());
- }
- };
-
- final private DynamicStyleMacro __LINE__ = new DynamicStyleMacro("__LINE__".toCharArray()) { //$NON-NLS-1$
- public Token execute() {
- int lineNumber= fLocationMap.getCurrentLineNumber(fCurrentContext.currentLexerToken().getOffset());
- return new TokenWithImage(IToken.tINTEGER, null, 0, 0, Long.toString(lineNumber).toCharArray());
- }
- };
-
final private IParserLogService fLog;
final private IIndexBasedCodeReaderFactory fCodeReaderFactory;
private final ExpressionEvaluator fExpressionEvaluator;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java
index 6e6b307f647..4eeacd6f5e5 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java
@@ -48,7 +48,7 @@ public class LocationMap implements ILocationResolver {
private ArrayList fDirectives= new ArrayList();
private ArrayList fProblems= new ArrayList();
private ArrayList fComments= new ArrayList();
- private ArrayList fBuiltinMacros= new ArrayList();
+ private ArrayList fBuiltinMacros= new ArrayList();
private ArrayList fMacroReferences= new ArrayList();
private LocationCtxFile fRootContext= null;
@@ -69,12 +69,12 @@ public class LocationMap implements ILocationResolver {
}
private void registerPredefinedMacro(IMacroBinding macro, IASTFileLocation nameloc, int expansionOffset) {
- ASTObjectStyleMacroDefinition astmacro;
+ ASTMacroDefinition astmacro;
if (macro.isFunctionStyle()) {
astmacro= new ASTFunctionStyleMacroDefinition(fTranslationUnit, macro, nameloc, expansionOffset);
}
else {
- astmacro= new ASTObjectStyleMacroDefinition(fTranslationUnit, macro, nameloc, expansionOffset);
+ astmacro= new ASTMacroDefinition(fTranslationUnit, macro, nameloc, expansionOffset);
}
fBuiltinMacros.add(astmacro);
}
@@ -291,7 +291,7 @@ public class LocationMap implements ILocationResolver {
endOffset= getSequenceNumberForOffset(endOffset);
ASTPreprocessorNode astMacro;
if (!macrodef.isFunctionStyle()) {
- astMacro= new ASTObjectStyleMacroDefinition(fTranslationUnit, macrodef, startOffset, nameOffset, nameEndOffset, expansionOffset, endOffset);
+ astMacro= new ASTMacroDefinition(fTranslationUnit, macrodef, startOffset, nameOffset, nameEndOffset, expansionOffset, endOffset);
}
else {
astMacro= new ASTFunctionStyleMacroDefinition(fTranslationUnit, macrodef, startOffset, nameOffset, nameEndOffset, expansionOffset, endOffset);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java
index 99b9b3af247..bf2adc5b036 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java
@@ -136,6 +136,11 @@ public class MacroExpander {
private int fStartOffset;
private int fEndOffset;
+ // for using the expander to track expansions
+ private String fFixedCurrentFilename;
+ private int fFixedLineNumber;
+ private char[] fFixedInput;
+
public MacroExpander(ILexerLog log, CharArrayMap macroDictionary, LocationMap locationMap, LexerOptions lexOptions) {
fDictionary= macroDictionary;
fLocationMap= locationMap;
@@ -173,10 +178,13 @@ public class MacroExpander {
* Method for tracking macro expansions.
* @since 5.0
*/
- public void expand(String beforeExpansion, MacroExpansionTracker tracker) {
- Lexer lexer= new Lexer(beforeExpansion.toCharArray(), fLexOptions, fLog, this);
+ public void expand(String beforeExpansion, MacroExpansionTracker tracker, String filePath, int lineNumber) {
fImplicitMacroExpansions.clear();
fImageLocationInfos.clear();
+ fFixedInput= beforeExpansion.toCharArray();
+ fFixedCurrentFilename= filePath;
+ fFixedLineNumber= lineNumber;
+ Lexer lexer= new Lexer(fFixedInput, fLexOptions, fLog, this);
try {
tracker.start(lexer);
@@ -362,17 +370,28 @@ public class MacroExpander {
return null;
}
- private void addSpacemarker(Token l, Token t, TokenList target) {
+ private static boolean isNeighborInSource(Token l, Token t) {
if (l != null && t != null) {
final Object s1= l.fSource;
final Object s2= t.fSource;
- if (s1 == s2 && s1 != null && l.getType() != CPreprocessor.tSPACE) {
- if (l.getEndOffset() == t.getOffset()) {
- target.append(new Token(CPreprocessor.tNOSPACE, null, 0, 0));
- }
- else {
- target.append(new Token(CPreprocessor.tSPACE, s1, l.getEndOffset(), t.getOffset()));
- }
+ return s1 == s2 && s1 != null &&
+ l.getType() != CPreprocessor.tSPACE && t.getType() != CPreprocessor.tSPACE;
+ }
+ return false;
+
+ }
+
+ static boolean hasImplicitSpace(Token l, Token t) {
+ return isNeighborInSource(l, t) && l.getEndOffset() != t.getOffset();
+ }
+
+ static void addSpacemarker(Token l, Token t, TokenList target) {
+ if (isNeighborInSource(l, t)) {
+ if (l.getEndOffset() == t.getOffset()) {
+ target.append(new Token(CPreprocessor.tNOSPACE, null, 0, 0));
+ }
+ else {
+ target.append(new Token(CPreprocessor.tSPACE, l.fSource, l.getEndOffset(), t.getOffset()));
}
}
}
@@ -397,7 +416,7 @@ public class MacroExpander {
boolean complete= false;
boolean isFirstOfArg= true;
Token lastToken= null;
- Token spaceMarker= null;
+ TokenList spaceMarkers= new TokenList();
loop: while (true) {
Token t= input.fetchFirst();
if (t == null) {
@@ -408,6 +427,7 @@ public class MacroExpander {
case IToken.tEND_OF_INPUT:
case IToken.tCOMPLETION:
case CPreprocessor.tSCOPE_MARKER:
+ case Lexer.tNEWLINE:
break;
default:
tracker.addFunctionStyleMacroExpansionToken((Token) t.clone());
@@ -449,7 +469,7 @@ public class MacroExpander {
if (nesting == 0) {
if (idx < argCount-1) { // next argument
isFirstOfArg= true;
- spaceMarker= null;
+ spaceMarkers.clear();
idx++;
continue loop;
}
@@ -471,7 +491,7 @@ public class MacroExpander {
case CPreprocessor.tSPACE:
case CPreprocessor.tNOSPACE:
if (!isFirstOfArg) {
- spaceMarker= t;
+ spaceMarkers.append(t);
}
continue loop;
@@ -481,10 +501,7 @@ public class MacroExpander {
if (argCount == 0) {
break loop;
}
- if (spaceMarker != null) {
- result[idx].append(spaceMarker);
- spaceMarker= null;
- }
+ result[idx].appendAll(spaceMarkers);
result[idx].append(t);
isFirstOfArg= false;
}
@@ -500,7 +517,7 @@ public class MacroExpander {
}
private void replaceArgs(PreprocessorMacro macro, TokenList[] args, TokenList[] expandedArgs, TokenList result) {
- TokenList replacement= clone(macro.getTokens(fDefinitionParser, fLexOptions));
+ TokenList replacement= clone(macro.getTokens(fDefinitionParser, fLexOptions, this));
Token l= null;
Token n;
@@ -638,7 +655,7 @@ public class MacroExpander {
private BitSet getParamUsage(PreprocessorMacro macro) {
final BitSet result= new BitSet();
- final TokenList replacement= macro.getTokens(fDefinitionParser, fLexOptions);
+ final TokenList replacement= macro.getTokens(fDefinitionParser, fLexOptions, this);
Token l= null;
Token n;
@@ -684,7 +701,7 @@ public class MacroExpander {
}
private void objStyleTokenPaste(PreprocessorMacro macro, TokenList result) {
- TokenList replacement= clone(macro.getTokens(fDefinitionParser, fLexOptions));
+ TokenList replacement= clone(macro.getTokens(fDefinitionParser, fLexOptions, this));
Token l= null;
Token n;
@@ -862,9 +879,33 @@ public class MacroExpander {
}
}
- static boolean hasImplicitSpace(Token l, Token t) {
- return l != null &&
- l.fSource != null && l.fSource == t.fSource &&
- l.getEndOffset() != t.getOffset() && t.getType() != CPreprocessor.tSPACE;
+ int getCurrentLineNumber() {
+ if (fFixedInput != null) {
+ return fFixedLineNumber + countNewlines(fFixedInput);
+ }
+ if (fLocationMap != null) {
+ return fLocationMap.getCurrentLineNumber(fEndOffset);
+ }
+ return 0;
+ }
+
+ private int countNewlines(char[] input) {
+ int nl= 0;
+ for (int i = 0; i < input.length && i fDictionary;
private MacroExpansionStep fFullExpansion;
private int fExpansionCount;
+ private String fFilePath;
+ private int fLineNumber;
- public SingleMacroExpansionExplorer(String input, IASTName ref, IASTName[] implicitRefs) {
+ public SingleMacroExpansionExplorer(String input, IASTName ref, IASTName[] implicitRefs, String filePath, int lineNumber) {
fInput= input;
fDictionary= createDictionary(ref, implicitRefs);
+ fFilePath= filePath;
+ fLineNumber= lineNumber;
}
private CharArrayMap createDictionary(IASTName ref, IASTName[] implicitRefs) {
- // mstodo handle dynamic style macros
- // mstodo clone index-macros
CharArrayMap map= new CharArrayMap(implicitRefs.length+1);
addMacroDefinition(map, ref);
for (IASTName name : implicitRefs) {
@@ -71,7 +73,7 @@ public class SingleMacroExpansionExplorer extends MacroExpansionExplorer {
private void computeExpansion() {
MacroExpander expander= new MacroExpander(ILexerLog.NULL, fDictionary, null, LEX_OPTIONS);
MacroExpansionTracker tracker= new MacroExpansionTracker(Integer.MAX_VALUE);
- expander.expand(fInput, tracker);
+ expander.expand(fInput, tracker, fFilePath, fLineNumber);
fExpansionCount= tracker.getStepCount();
ReplaceEdit r= tracker.getReplacement();
@@ -87,7 +89,7 @@ public class SingleMacroExpansionExplorer extends MacroExpansionExplorer {
}
MacroExpander expander= new MacroExpander(ILexerLog.NULL, fDictionary, null, LEX_OPTIONS);
MacroExpansionTracker tracker= new MacroExpansionTracker(step);
- expander.expand(fInput, tracker);
+ expander.expand(fInput, tracker, fFilePath, fLineNumber);
fExpansionCount= tracker.getStepCount();
ReplaceEdit r= tracker.getReplacement();
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/TokenList.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/TokenList.java
index 3ba2d02bffb..4d86f607b93 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/TokenList.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/TokenList.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007 Wind River Systems, Inc. and others.
+ * Copyright (c) 2007, 2008 Wind River Systems, Inc. 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
@@ -121,4 +121,8 @@ class TokenList {
fLast= l;
}
}
+
+ public void clear() {
+ fFirst= fLast= 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 231341e9bbd..7ac2af8fc26 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
@@ -173,7 +173,7 @@ public class PDOMMacro implements IIndexMacro, IASTFileLocation {
fExpansion= getExpansionInDB().getChars();
} catch (CoreException e) {
CCorePlugin.log(e);
- fExpansion= new char[] { ' ' };
+ fExpansion= new char[] {};
}
}
return fExpansion;