diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java index 0c09a497294..6443534bdd1 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java @@ -8,6 +8,7 @@ * Contributors: * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -45,6 +46,7 @@ public class DOMParserTestSuite extends TestCase { suite.addTestSuite( AST2CSpecFailingTest.class ); suite.addTestSuite( DOMSelectionParseTest.class ); suite.addTestSuite( GCCCompleteParseExtensionsTest.class ); + suite.addTestSuite(DOMPreprocessorInformationTest.class); suite.addTest( CompletionTestSuite.suite() ); return suite; } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java new file mode 100644 index 00000000000..512cca3bb71 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * Copyright (c) 2007 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences 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: + * Emanuel Graf - initial API and implementation + ******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.ast2; + +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorElifStatement; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorErrorStatement; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfStatement; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfdefStatement; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfndefStatement; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorPragmaStatement; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.parser.ParserLanguage; + +/** + * @author Emanuel Graf + * + */ +public class DOMPreprocessorInformationTest extends AST2BaseTest { + + public void testPragma() throws Exception { + String msg = "GCC poison printf sprintf fprintf"; + StringBuffer buffer = new StringBuffer( "#pragma " + msg + "\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(1, st.length); + assertTrue(st[0] instanceof IASTPreprocessorPragmaStatement); + IASTPreprocessorPragmaStatement pragma = (IASTPreprocessorPragmaStatement) st[0]; + assertEquals(msg, new String(pragma.getMessage())); + } + + public void testElIf() throws Exception { + String cond = "2 == 2"; + StringBuffer buffer = new StringBuffer( "#if 1 == 2\n#elif " + cond + "\n#else\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(4, st.length); + assertTrue(st[1] instanceof IASTPreprocessorElifStatement); + IASTPreprocessorElifStatement pragma = (IASTPreprocessorElifStatement) st[1]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testIf() throws Exception { + String cond = "2 == 2"; + StringBuffer buffer = new StringBuffer( "#if " + cond + "\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(2, st.length); + assertTrue(st[0] instanceof IASTPreprocessorIfStatement); + IASTPreprocessorIfStatement pragma = (IASTPreprocessorIfStatement) st[0]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testIfDef() throws Exception{ + String cond = "SYMBOL"; + StringBuffer buffer = new StringBuffer( "#ifdef " + cond + "\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(2, st.length); + assertTrue(st[0] instanceof IASTPreprocessorIfdefStatement); + IASTPreprocessorIfdefStatement pragma = (IASTPreprocessorIfdefStatement) st[0]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testIfnDef() throws Exception{ + String cond = "SYMBOL"; + StringBuffer buffer = new StringBuffer( "#ifndef " + cond + "\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(2, st.length); + assertTrue(st[0] instanceof IASTPreprocessorIfndefStatement); + IASTPreprocessorIfndefStatement pragma = (IASTPreprocessorIfndefStatement) st[0]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testError() throws Exception{ + String msg = "Message"; + StringBuffer buffer = new StringBuffer( "#error " + msg + "\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP, false, false ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(1, st.length); + assertTrue(st[0] instanceof IASTPreprocessorErrorStatement); + IASTPreprocessorErrorStatement pragma = (IASTPreprocessorErrorStatement) st[0]; + assertEquals(msg, new String(pragma.getMessage())); + } + + public void testPragmaWithSpaces() throws Exception { + String msg = "GCC poison printf sprintf fprintf"; + StringBuffer buffer = new StringBuffer( "# pragma " + msg + "\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(1, st.length); + assertTrue(st[0] instanceof IASTPreprocessorPragmaStatement); + IASTPreprocessorPragmaStatement pragma = (IASTPreprocessorPragmaStatement) st[0]; + assertEquals(msg, new String(pragma.getMessage())); + } + + public void testElIfWithSpaces() throws Exception { + String cond = "2 == 2"; + StringBuffer buffer = new StringBuffer( "#if 1 == 2\n# elif " + cond + "\n#else\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(4, st.length); + assertTrue(st[1] instanceof IASTPreprocessorElifStatement); + IASTPreprocessorElifStatement pragma = (IASTPreprocessorElifStatement) st[1]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testIfWithSpaces() throws Exception { + String cond = "2 == 2"; + StringBuffer buffer = new StringBuffer( "# if " + cond + "\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(2, st.length); + assertTrue(st[0] instanceof IASTPreprocessorIfStatement); + IASTPreprocessorIfStatement pragma = (IASTPreprocessorIfStatement) st[0]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testIfDefWithSpaces() throws Exception{ + String cond = "SYMBOL"; + StringBuffer buffer = new StringBuffer( "# ifdef " + cond + "\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(2, st.length); + assertTrue(st[0] instanceof IASTPreprocessorIfdefStatement); + IASTPreprocessorIfdefStatement pragma = (IASTPreprocessorIfdefStatement) st[0]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testIfnDefWithSpaces() throws Exception{ + String cond = "SYMBOL"; + StringBuffer buffer = new StringBuffer( "# ifndef " + cond + "\n#endif\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(2, st.length); + assertTrue(st[0] instanceof IASTPreprocessorIfndefStatement); + IASTPreprocessorIfndefStatement pragma = (IASTPreprocessorIfndefStatement) st[0]; + assertEquals(cond, new String(pragma.getCondition())); + } + + public void testErrorWithSpaces() throws Exception{ + String msg = "Message"; + StringBuffer buffer = new StringBuffer( "# error " + msg + "\n" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP, false, false ); + IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements(); + assertEquals(1, st.length); + assertTrue(st[0] instanceof IASTPreprocessorErrorStatement); + IASTPreprocessorErrorStatement pragma = (IASTPreprocessorErrorStatement) st[0]; + assertEquals(msg, new String(pragma.getMessage())); + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorElifStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorElifStatement.java index c89186fdb83..cbb2b414d72 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorElifStatement.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorElifStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -24,5 +25,12 @@ public interface IASTPreprocessorElifStatement extends * @return boolean */ public boolean taken(); + + /** + * The condition of the elif. + * + * @return the Condition + */ + public char[] getCondition(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorErrorStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorErrorStatement.java index 53a3cc69922..9d2ed28b193 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorErrorStatement.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorErrorStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -17,5 +18,11 @@ package org.eclipse.cdt.core.dom.ast; */ public interface IASTPreprocessorErrorStatement extends IASTPreprocessorStatement { + /** + * The Error Message. + * + * @return the Message + */ + public char[] getMessage(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfStatement.java index 608209404e7..64f6f6ad028 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfStatement.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -23,4 +24,11 @@ public interface IASTPreprocessorIfStatement extends IASTPreprocessorStatement { * @return boolean */ public boolean taken(); + + /** + * The condition of the if. + * + * @return the Condition + */ + public char[] getCondition(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfdefStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfdefStatement.java index 77e39023a7c..43e2011baec 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfdefStatement.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfdefStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -24,4 +25,11 @@ public interface IASTPreprocessorIfdefStatement extends * @return */ public boolean taken(); + + /** + * The condition of the ifdef. + * + * @return the Condition + */ + public char[] getCondition(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfndefStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfndefStatement.java index 938a7d69cd0..961cb517091 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfndefStatement.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorIfndefStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -24,4 +25,10 @@ public interface IASTPreprocessorIfndefStatement extends * @return */ public boolean taken(); + /** + * The condition of the ifndef. + * + * @return the Condition + */ + public char[] getCondition(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorPragmaStatement.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorPragmaStatement.java index f5d30ea8d65..917db6e1357 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorPragmaStatement.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTPreprocessorPragmaStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -17,5 +18,11 @@ package org.eclipse.cdt.core.dom.ast; */ public interface IASTPreprocessorPragmaStatement extends IASTPreprocessorStatement { + + /** + * Returns the pragma message. + * @return + */ + public char[] getMessage(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java index a97a66a8365..7770ca94a16 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java @@ -10,6 +10,7 @@ * Markus Schorn (Wind River Systems) * Bryan Wilkinson (QNX) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151207 * Anton Leherbauer (Wind River Systems) + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner2; @@ -115,6 +116,18 @@ abstract class BaseScanner implements IScanner { return endOffset-startOffset; } } + + protected static class FunctionMacroData extends MacroData{ + private CharArrayObjectMap arguments; + public FunctionMacroData(int start, int end, IMacro macro, CharArrayObjectMap argmap) { + super(start,end, macro); + arguments = argmap; + } + + public CharArrayObjectMap getActualArgs() { + return arguments; + } + } protected ParserLanguage language; @@ -3269,7 +3282,8 @@ abstract class BaseScanner implements IScanner { } if (pushContext) { - pushContext(result, new MacroData(start, bufferPos[bufferStackPos]+1, macro)); + pushContext(result, new FunctionMacroData(start, bufferPos[bufferStackPos] + 1, + macro, argmap)); } return result; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java index 25f690c0c96..a85515ffe45 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java @@ -9,6 +9,7 @@ * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) * Anton Leherbauer (Wind River Systems) + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner2; @@ -208,6 +209,7 @@ public class DOMScanner extends BaseScanner { if (data instanceof InclusionData) { + InclusionData inclusionData = ((InclusionData) data); if (log.isTracing()) { StringBuffer b = new StringBuffer("Entering inclusion "); //$NON-NLS-1$ b.append(((InclusionData) data).reader.filename); @@ -215,7 +217,7 @@ public class DOMScanner extends BaseScanner { } if( ! isCircularInclusion( (InclusionData) data )) { - DOMInclusion inc = ((DOMInclusion) ((InclusionData) data).inclusion); + DOMInclusion inc = ((DOMInclusion) inclusionData.inclusion); locationMap.startInclusion(((InclusionData) data).reader, inc.o, getGlobalOffset(getCurrentOffset())+1, inc.nameOffset, inc.nameEndoffset, inc.name, inc.systemInclude); bufferDelta[bufferStackPos + 1] = 0; @@ -225,11 +227,11 @@ public class DOMScanner extends BaseScanner { else if (data instanceof MacroData) { MacroData d = (MacroData) data; if (d.macro instanceof FunctionStyleMacro && fsmCount == 0) { + FunctionMacroData fd = (FunctionMacroData)d; FunctionStyleMacro fsm = (FunctionStyleMacro) d.macro; - int startOffset= getGlobalOffset(d.getStartOffset()); - int endOffset= startOffset+d.getLength(); locationMap.startFunctionStyleExpansion(fsm.attachment, - fsm.arglist, startOffset, endOffset); + fsm.arglist, getGlobalOffset(d.getStartOffset()), + getGlobalOffset(d.getStartOffset() + d.getLength()),fd.getActualArgs().valueArray() ); bufferDelta[bufferStackPos + 1] = 0; } else if (d.macro instanceof ObjectStyleMacro && fsmCount == 0) { ObjectStyleMacro osm = (ObjectStyleMacro) d.macro; @@ -395,12 +397,18 @@ public class DOMScanner extends BaseScanner { */ protected void processIfdef(int startPos, int endPos, boolean positive, boolean taken) { - if (positive) + if (positive){ + int startCond = startPos + 7 + countSpaces(startPos); + char[] condition = CharArrayUtils.extract(bufferStack[bufferStackPos], startCond, endPos - startCond); locationMap.encounterPoundIfdef(getGlobalOffset(startPos), - getGlobalOffset(endPos), taken); - else + getGlobalOffset(endPos), taken, condition); + } + else{ + int startCond = startPos + 8 + countSpaces(startPos); + char[] condition = CharArrayUtils.extract(bufferStack[bufferStackPos], startCond, endPos - startCond); locationMap.encounterPoundIfndef(getGlobalOffset(startPos), - getGlobalOffset(endPos), taken); + getGlobalOffset(endPos), taken, condition); + } } @@ -411,8 +419,10 @@ public class DOMScanner extends BaseScanner { * int, boolean) */ protected void processIf(int startPos, int endPos, boolean taken) { + int startCond = startPos + 4 + countSpaces(startPos); + char[] condition = CharArrayUtils.extract(bufferStack[bufferStackPos], startCond, endPos - startCond); locationMap.encounterPoundIf(getGlobalOffset(startPos), - getGlobalOffset(endPos), taken); + getGlobalOffset(endPos), taken, condition); } /* @@ -422,8 +432,10 @@ public class DOMScanner extends BaseScanner { * int, boolean) */ protected void processElsif(int startPos, int endPos, boolean taken) { + int startCond = startPos + 6 + countSpaces(startPos); + char[] condition = CharArrayUtils.extract(bufferStack[bufferStackPos], startCond, endPos - startCond); locationMap.encounterPoundElif(getGlobalOffset(startPos), - getGlobalOffset(endPos), taken); + getGlobalOffset(endPos), taken, condition); } /* @@ -458,16 +470,28 @@ public class DOMScanner extends BaseScanner { * int) */ protected void processError(int startPos, int endPos) { + int start = startPos+7 + countSpaces(startPos); + char[] msg = CharArrayUtils.extract(bufferStack[bufferStackPos], start, endPos- start); locationMap.encounterPoundError(getGlobalOffset(startPos), - getGlobalOffset(endPos)); + getGlobalOffset(endPos), msg); } /* * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#processWarning(int, int) */ protected void processWarning(int startPos, int endPos) { + int start = startPos+9 + countSpaces(startPos); + char[] msg = CharArrayUtils.extract(bufferStack[bufferStackPos], start, endPos - start); locationMap.encounterPoundWarning(getGlobalOffset(startPos), - getGlobalOffset(endPos)); + getGlobalOffset(endPos), msg); + } + + private int countSpaces(int startPos) { + int spaces = 0; + while(bufferStack[bufferStackPos][startPos + spaces + 1] == ' ' || bufferStack[bufferStackPos][startPos + spaces + 1] == '\t' ) { + ++spaces; + } + return spaces; } /* @@ -487,7 +511,9 @@ public class DOMScanner extends BaseScanner { * int) */ protected void processPragma(int startPos, int endPos) { - locationMap.encounterPoundPragma(getGlobalOffset(startPos), getGlobalOffset(endPos)); + int startCond = startPos + 8 + countSpaces(startPos); + char[] msg = CharArrayUtils.extract(bufferStack[bufferStackPos], startCond, endPos - (startCond)); + locationMap.encounterPoundPragma(getGlobalOffset(startPos), getGlobalOffset(endPos), msg); } protected void beforeReplaceAllMacros() { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IScannerPreprocessorLog.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IScannerPreprocessorLog.java index 167ca3db5c6..d0d5ad74900 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IScannerPreprocessorLog.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IScannerPreprocessorLog.java @@ -9,6 +9,7 @@ * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) * Anton Leherbauer (Wind River Systems) + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner2; @@ -36,7 +37,7 @@ public interface IScannerPreprocessorLog { public void startFunctionStyleExpansion(IMacroDefinition macro, - char[][] parameters, int startOffset, int endOffset); + char[][] parameters, int startOffset, int endOffset, Object[] objects); public void endFunctionStyleExpansion(IMacroDefinition macro, int offset); @@ -54,25 +55,25 @@ public interface IScannerPreprocessorLog { public IMacroDefinition defineFunctionStyleMacro(FunctionStyleMacro m, int startOffset, int nameOffset, int nameEndOffset, int endOffset); - public void encounterPoundIf(int startOffset, int endOffset, boolean taken); + public void encounterPoundIf(int startOffset, int endOffset, boolean taken, char[] condition); public void encounterPoundIfdef(int startOffset, int endOffset, - boolean taken); + boolean taken, char[] condition); public void encounterPoundIfndef(int startOffset, int endOffset, - boolean taken); + boolean taken, char[] condition); public void encounterPoundElse(int startOffset, int endOffset, boolean taken); - public void encounterPoundElif(int startOffset, int endOffset, boolean taken); + public void encounterPoundElif(int startOffset, int endOffset, boolean taken, char[] condition); public void encounterPoundEndIf(int startOffset, int endOffset); - public void encounterPoundPragma(int startOffset, int endOffset); + public void encounterPoundPragma(int startOffset, int endOffset, char[] msg); - public void encounterPoundError(int startOffset, int endOffset); + public void encounterPoundError(int startOffset, int endOffset, char[] msg); - public void encounterPoundWarning(int startOffset, int endOffset); + public void encounterPoundWarning(int startOffset, int endOffset, char[] msg); public void encounterPoundUndef(int startOffset, int endOffset, char[] symbol, int nameOffset, IMacroDefinition macroDefinition); 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 be91d224f1a..9e196ee3434 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 @@ -9,6 +9,7 @@ * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) * Anton Leherbauer (Wind River Systems) + * Emanuel Graf (IFS) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner2; @@ -106,6 +107,27 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } } + + public class FunctionMacroExpansionLocation extends MacroExpansionLocation{ + + private Object[] actParams; + + /** + * @param macroDefinition + * @param locations + * @param offset + * @param length + */ + public FunctionMacroExpansionLocation(IASTPreprocessorMacroDefinition macroDefinition, IASTNodeLocation[] locations, int offset, int length, Object[] actParameters) { + super(macroDefinition, locations, offset, length); + this.actParams = actParameters; + } + + public Object[] getActualParameters() { + return actParams; + } + + } private static final String NOT_VALID_MACRO = "Not a valid macro selection"; //$NON-NLS-1$ @@ -126,6 +148,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { IASTPreprocessorElifStatement { private final boolean taken; + private char[] condition; /* * (non-Javadoc) @@ -139,10 +162,18 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { /** * @param taken */ - public ASTElif(boolean taken) { + public ASTElif(boolean taken, char[] condition) { this.taken = taken; + this.condition = condition; } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IASTPreprocessorElifStatement#getCondition() + */ + public char[] getCondition() { + return condition; + } + } /** @@ -177,6 +208,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { IASTPreprocessorIfndefStatement { private final boolean taken; + private char[] condition; /* * (non-Javadoc) @@ -190,10 +222,18 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { /** * @param taken */ - public ASTIfndef(boolean taken) { + public ASTIfndef(boolean taken, char[] condition) { this.taken = taken; + this.condition = condition; } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfndefStatement#getCondition() + */ + public char[] getCondition() { + return condition; + } + } /** @@ -203,6 +243,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { IASTPreprocessorIfdefStatement { private final boolean taken; + + private char[] condition; /* * (non-Javadoc) @@ -216,10 +258,18 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { /** * @param taken */ - public ASTIfdef(boolean taken) { + public ASTIfdef(boolean taken, char[] condition) { this.taken = taken; + this.condition = condition; } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfdefStatement#getCondition() + */ + public char[] getCondition() { + return condition; + } + } /** @@ -229,6 +279,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { IASTPreprocessorIfStatement { private final boolean taken; + private char[] condition; /* * (non-Javadoc) @@ -242,10 +293,18 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { /** * @param taken */ - public ASTIf(boolean taken) { + public ASTIf(boolean taken, char[] condition) { this.taken = taken; + this.condition = condition; } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfStatement#getCondition() + */ + public char[] getCondition() { + return condition; + } + } /** @@ -253,11 +312,35 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ public static class ASTError extends ASTNode implements IASTPreprocessorErrorStatement { + + private char[] msg; + + + + public ASTError(char[] msg) { + super(); + this.msg = msg; + } + + public char[] getMessage() { + return msg; + } } public static class ASTWarning extends ASTNode implements - IASTPreprocessorErrorStatement { + IASTPreprocessorErrorStatement { + + private char[] msg; + + public ASTWarning(char[] msg) { + super(); + this.msg = msg; + } + + public char[] getMessage() { + return msg; + } } /** @@ -265,6 +348,24 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ public static class ASTPragma extends ASTNode implements IASTPreprocessorPragmaStatement { + + private char[] msg; + + + + public ASTPragma(char[] msg) { + super(); + this.msg = msg; + } + + + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IASTPreprocessorPragmaStatement#getMessage() + */ + public char[] getMessage() { + return msg; + } } @@ -314,6 +415,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { _IPreprocessorDirective { public final boolean taken; + public char[] condition; /** * @param parent @@ -321,9 +423,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @param endOffset */ public _Elif(_CompositeContext parent, int startOffset, int endOffset, - boolean taken) { + boolean taken, char[] condition) { super(parent, startOffset, endOffset); this.taken = taken; + this.condition = condition; } } @@ -335,6 +438,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { _IPreprocessorDirective { public final boolean taken; + public char[] condition; /** * @param parent @@ -342,9 +446,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @param endOffset */ public _Ifdef(_CompositeContext parent, int startOffset, int endOffset, - boolean taken) { + boolean taken, char[] condition) { super(parent, startOffset, endOffset); this.taken = taken; + this.condition = condition; } } @@ -356,6 +461,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { _IPreprocessorDirective { public final boolean taken; + public char[] condition; /** * @param parent @@ -363,9 +469,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @param endOffset */ public _Ifndef(_CompositeContext parent, int startOffset, - int endOffset, boolean taken) { + int endOffset, boolean taken, char[] condition) { super(parent, startOffset, endOffset); this.taken = taken; + this.condition = condition; } } @@ -375,22 +482,27 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ protected static class _Error extends _Context implements _IPreprocessorDirective { + + char[] msg; /** * @param parent * @param startOffset * @param endOffset + * @param msg */ - public _Error(_CompositeContext parent, int startOffset, int endOffset) { + public _Error(_CompositeContext parent, int startOffset, int endOffset, char[] msg) { super(parent, startOffset, endOffset); - // TODO Auto-generated constructor stub + this.msg = msg; } } protected static class _Warning extends _Context implements _IPreprocessorDirective { - public _Warning(_CompositeContext parent, int startOffset, int endOffset) { + char[] msg; + public _Warning(_CompositeContext parent, int startOffset, int endOffset, char[] msg) { super(parent, startOffset, endOffset); + this.msg = msg; } } @@ -399,15 +511,19 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ protected static class _Pragma extends _Context implements _IPreprocessorDirective { + + char[] msg; /** * @param parent * @param startOffset * @param endOffset */ - public _Pragma(_CompositeContext parent, int startOffset, int endOffset) { + public _Pragma(_CompositeContext parent, int startOffset, int endOffset, char[] msg) { super(parent, startOffset, endOffset); + this.msg = msg; } + } @@ -417,6 +533,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { protected class _If extends _Context implements _IPreprocessorDirective { public final boolean taken; + public char[] condition; /** * @param parent @@ -424,9 +541,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @param endOffset */ public _If(_CompositeContext parent, int startOffset, int endOffset, - boolean taken) { + boolean taken, char[] condition) { super(parent, startOffset, endOffset); this.taken = taken; + this.condition = condition; } } @@ -1461,13 +1579,15 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { protected class _FunctionMacroExpansion extends _MacroExpansion { public final char[][] args; + public final Object[] actArgs; public _FunctionMacroExpansion(_CompositeContext parent, int startOffset, int endOffset, IMacroDefinition definition, - char[][] args) { + char[][] args, Object[] actParameters) { super(parent, startOffset, endOffset, definition); this.args = args; + this.actArgs = actParameters; } } @@ -1703,7 +1823,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTElif(_Elif elif) { - IASTPreprocessorElifStatement result = new ASTElif(elif.taken); + IASTPreprocessorElifStatement result = new ASTElif(elif.taken, elif.condition); ((ASTNode) result).setOffsetAndLength(elif.context_directive_start, elif.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1727,7 +1847,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTIfndef(_Ifndef ifndef) { - IASTPreprocessorIfndefStatement result = new ASTIfndef(ifndef.taken); + IASTPreprocessorIfndefStatement result = new ASTIfndef(ifndef.taken, ifndef.condition); ((ASTNode) result).setOffsetAndLength(ifndef.context_directive_start, ifndef.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1739,7 +1859,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTIfdef(_Ifdef ifdef) { - IASTPreprocessorIfdefStatement result = new ASTIfdef(ifdef.taken); + IASTPreprocessorIfdefStatement result = new ASTIfdef(ifdef.taken, ifdef.condition); ((ASTNode) result).setOffsetAndLength(ifdef.context_directive_start, ifdef.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1751,7 +1871,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTIf(_If i) { - IASTPreprocessorIfStatement result = new ASTIf(i.taken); + IASTPreprocessorIfStatement result = new ASTIf(i.taken, i.condition); ((ASTNode) result).setOffsetAndLength(i.context_directive_start, i.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1763,7 +1883,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTError(_Error error) { - IASTPreprocessorErrorStatement result = new ASTError(); + IASTPreprocessorErrorStatement result = new ASTError(error.msg); ((ASTNode) result).setOffsetAndLength(error.context_directive_start, error.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1775,7 +1895,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTWarning(_Warning warning) { - IASTPreprocessorErrorStatement result = new ASTWarning(); + IASTPreprocessorErrorStatement result = new ASTWarning(warning.msg); ((ASTNode) result).setOffsetAndLength(warning.context_directive_start, warning.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1787,7 +1907,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @return */ private IASTPreprocessorStatement createASTPragma(_Pragma pragma) { - IASTPreprocessorPragmaStatement result = new ASTPragma(); + IASTPreprocessorPragmaStatement result = new ASTPragma(pragma.msg); ((ASTNode) result).setOffsetAndLength(pragma.getDirectiveStart(), pragma.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1952,7 +2072,14 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { definition = astNode; } - return new MacroExpansionLocation(definition, locations, expansion.getOffsetInContext(offset), length); + if (c instanceof _FunctionMacroExpansion) { + _FunctionMacroExpansion fe = (_FunctionMacroExpansion) c; + return new FunctionMacroExpansionLocation(definition, locations, fe.getOffsetInContext(offset), length, fe.actArgs); + }else { + _MacroExpansion me = (_MacroExpansion) c; + return new MacroExpansionLocation(definition, locations, + me.getOffsetInContext(offset), length); + } } return null; } @@ -2085,9 +2212,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * char[][], char[], int) */ public void startFunctionStyleExpansion(IMacroDefinition macro, - char[][] parameters, int startOffset, int endOffset) { + char[][] parameters, int startOffset, int endOffset, Object[] actParameters) { _FunctionMacroExpansion context = new _FunctionMacroExpansion( - currentContext, startOffset, endOffset, macro, parameters); + currentContext, startOffset, endOffset, macro, parameters, actParameters); currentContext.addSubContext(context); currentContext = context; } @@ -2174,9 +2301,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterPoundIf(int, * int) */ - public void encounterPoundIf(int startOffset, int endOffset, boolean taken) { + public void encounterPoundIf(int startOffset, int endOffset, boolean taken, char[] condtion) { currentContext.addSubContext(new _If(currentContext, startOffset, - endOffset, taken)); + endOffset, taken, condtion)); } @@ -2186,9 +2313,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterPoundPragma(int, * int) */ - public void encounterPoundPragma(int startOffset, int endOffset) { + public void encounterPoundPragma(int startOffset, int endOffset, char[] msg) { currentContext.addSubContext(new _Pragma(currentContext, startOffset, - endOffset)); + endOffset, msg)); } /* @@ -2197,17 +2324,17 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterPoundError(int, * int) */ - public void encounterPoundError(int startOffset, int endOffset) { + public void encounterPoundError(int startOffset, int endOffset, char[] msg) { currentContext.addSubContext(new _Error(currentContext, startOffset, - endOffset)); + endOffset, msg)); } /* * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterPoundWarning(int, int) */ - public void encounterPoundWarning(int startOffset, int endOffset) { + public void encounterPoundWarning(int startOffset, int endOffset, char[] msg) { currentContext.addSubContext(new _Warning(currentContext, startOffset, - endOffset)); + endOffset, msg)); } /* @@ -2217,9 +2344,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * int) */ public void encounterPoundIfdef(int startOffset, int endOffset, - boolean taken) { + boolean taken, char[] condition) { currentContext.addSubContext(new _Ifdef(currentContext, startOffset, - endOffset, taken)); + endOffset, taken, condition)); } /* @@ -2251,9 +2378,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterPoundElif(int, * int) */ - public void encounterPoundElif(int startOffset, int endOffset, boolean taken) { + public void encounterPoundElif(int startOffset, int endOffset, boolean taken, char[] condition) { currentContext.addSubContext(new _Elif(currentContext, startOffset, - endOffset, taken)); + endOffset, taken, condition)); } /* @@ -2470,9 +2597,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { * int, boolean) */ public void encounterPoundIfndef(int startOffset, int endOffset, - boolean taken) { + boolean taken, char[] condition) { currentContext.addSubContext(new _Ifndef(currentContext, startOffset, - endOffset, taken)); + endOffset, taken, condition)); } _Inclusion findInclusion(_CompositeContext context, String path) {