From 92b49872f2c9895f7f9b9a4e19f3e752f1acf50a Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 2 Nov 2006 14:15:37 +0000 Subject: [PATCH] Fix for 162180, scanner locations --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 20 +- .../tests/ast2/DOMLocationInclusionTests.java | 63 +++- .../tests/ast2/DOMLocationMacroTests.java | 9 +- .../parser/tests/ast2/DOMLocationTests.java | 132 +++++++- .../parser/tests/ast2/DOMParserTestSuite.java | 5 +- .../core/dom/parser/c/GNUCSourceParser.java | 11 +- .../core/parser/scanner2/BaseScanner.java | 295 +++++++++++------- .../core/parser/scanner2/DOMScanner.java | 164 +++++----- .../core/parser/scanner2/LocationMap.java | 270 +++++++--------- .../core/parser/scanner2/Scanner2.java | 12 +- 10 files changed, 580 insertions(+), 401 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 6c2f52cb212..1830cd2f751 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -3379,8 +3380,9 @@ public class AST2Tests extends AST2BaseTest { buffer.append("((int arg1)){\r\n"); //$NON-NLS-1$ buffer.append("return 0;\r\n"); //$NON-NLS-1$ buffer.append("}\r\n"); //$NON-NLS-1$ - parse(buffer.toString(), ParserLanguage.C); - IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C); + assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); + tu = parse(buffer.toString(), ParserLanguage.CPP); assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); buffer = new StringBuffer(); @@ -3392,8 +3394,9 @@ public class AST2Tests extends AST2BaseTest { buffer.append("((int arg1)){\n"); //$NON-NLS-1$ buffer.append("return 0;\n"); //$NON-NLS-1$ buffer.append("}\n"); //$NON-NLS-1$ - parse(buffer.toString(), ParserLanguage.C); - tu = parse(buffer.toString(), ParserLanguage.CPP); + tu= parse(buffer.toString(), ParserLanguage.C); + assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); + tu= parse(buffer.toString(), ParserLanguage.CPP); assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); } @@ -3407,8 +3410,10 @@ public class AST2Tests extends AST2BaseTest { buffer.append("((int arg1)){\r\n"); //$NON-NLS-1$ buffer.append("return 0;\r\n"); //$NON-NLS-1$ buffer.append("}\r\n"); //$NON-NLS-1$ - parse(buffer.toString(), ParserLanguage.C); - IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + IASTTranslationUnit tu= parse(buffer.toString(), ParserLanguage.C); + assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); + + tu = parse(buffer.toString(), ParserLanguage.CPP); assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); buffer = new StringBuffer(); @@ -3420,7 +3425,8 @@ public class AST2Tests extends AST2BaseTest { buffer.append("((int arg1)){\n"); //$NON-NLS-1$ buffer.append("return 0;\n"); //$NON-NLS-1$ buffer.append("}\n"); //$NON-NLS-1$ - parse(buffer.toString(), ParserLanguage.C); + tu = parse(buffer.toString(), ParserLanguage.C); + assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); tu = parse(buffer.toString(), ParserLanguage.CPP); assertFalse( tu.getDeclarations()[1] instanceof IASTProblemDeclaration ); } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java index 01fced426ba..50891171ea7 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2006 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 + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -214,11 +215,7 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest { IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives(); assertNotNull(incs); assertEquals(incs.length, 1); - assertSoleFileLocation( - incs[0], - filename, - code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ - + assertSoleFileLocation(incs[0], filename, code.indexOf("#inc"), "#include \"foo.h\"".length()); } } @@ -452,8 +449,7 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest { assertEquals(locs.length, 1); IASTFileLocation fileLoc = (IASTFileLocation) locs[0]; assertEquals(code.indexOf("#include"), fileLoc.getNodeOffset()); //$NON-NLS-1$ - assertEquals( - "#include \n".length(), fileLoc.getNodeLength()); //$NON-NLS-1$ + assertEquals("#include ".length(), fileLoc.getNodeLength()); //$NON-NLS-1$ } } @@ -465,8 +461,7 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest { } public void testBug97603() throws Exception { - IFile imacro_file = importFile( - "macro.h", "#define JEDEN 1\n#define DVA 2\n#define TRI 3\n"); //$NON-NLS-1$ //$NON-NLS-2$ + IFile imacro_file = importFile("macro.h", "#define JEDEN 1\n#define DVA 2\n#define TRI 3\n"); //$NON-NLS-1$ //$NON-NLS-2$ StringBuffer buffer = new StringBuffer(); buffer.append("#ifndef _INCLUDE_H_\n"); //$NON-NLS-1$ buffer.append("#define _INCLUDE_H_\n"); //$NON-NLS-1$ @@ -511,4 +506,52 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest { } } + + public void testBug97603_2() throws Exception { + IFile imacro_file1= importFile("macro1.h", "#define JEDEN 1\n"); + IFile imacro_file2= importFile("macro2.h", "#define DVA 2\n#define TRI 3\n"); + StringBuffer buffer = new StringBuffer(); + buffer.append("#ifndef _INCLUDE_H_\n"); //$NON-NLS-1$ + buffer.append("#define _INCLUDE_H_\n"); //$NON-NLS-1$ + buffer.append("typedef void (*vfp)();\n"); //$NON-NLS-1$ + buffer.append("typedef int (*ifp)();\n"); //$NON-NLS-1$ + buffer.append("struct Include {\n"); //$NON-NLS-1$ + buffer.append("int i;\n"); //$NON-NLS-1$ + buffer.append("};\n"); //$NON-NLS-1$ + buffer.append("#endif /*_INCLUDE_H_*/\n"); //$NON-NLS-1$ + final String inc_file_code = buffer.toString(); + IFile include_file = importFile("include.h", inc_file_code); //$NON-NLS-1$ //$NON-NLS-2$ + String[] macros = { imacro_file1.getLocation().toOSString(), imacro_file2.getLocation().toOSString() }; + String[] includes = { include_file.getLocation().toOSString() }; + IExtendedScannerInfo scannerInfo = new ExtendedScannerInfo( + Collections.EMPTY_MAP, EMPTY_STRING_ARRAY, macros, includes); + for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP + : null) { + String filename = (p == ParserLanguage.CPP) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$ + IFile code = importFile(filename, + "int main() { return BEAST * sizeof( Include ); } "); //$NON-NLS-1$ //$NON-NLS-2$ + + IASTTranslationUnit tu = parse(code, scannerInfo); //$NON-NLS-1$ + IASTPreprocessorMacroDefinition[] macro_defs = tu + .getMacroDefinitions(); + assertEquals(macro_defs.length, 4); + IASTPreprocessorMacroDefinition BEAST = macro_defs[0]; + assertEquals(BEAST.getName().toString(), "JEDEN"); //$NON-NLS-1$ + IASTPreprocessorMacroDefinition INCLUDE_H = macro_defs[3]; + final IASTNodeLocation[] nodeLocations = INCLUDE_H.getName() + .getNodeLocations(); + assertEquals(nodeLocations.length, 1); + final IASTFileLocation flatLoc = INCLUDE_H.getName() + .getFileLocation(); + assertNotNull(flatLoc); + assertEquals(include_file.getLocation().toOSString(), flatLoc + .getFileName()); + assertEquals( + inc_file_code.indexOf("#define _INCLUDE_H_") + "#define ".length(), flatLoc.getNodeOffset()); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals("_INCLUDE_H_".length(), flatLoc.getNodeLength()); //$NON-NLS-1$ + for (int j = 0; j < macro_defs.length; ++j) + assertNotNull(macro_defs[j].getName().getFileLocation()); + + } + } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java index 1bbf28711c6..bde132fdb44 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2006 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 + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -113,15 +114,15 @@ public class DOMLocationMacroTests extends AST2BaseTest { for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP : null) { IASTTranslationUnit tu = parse(code, p); - IASTPreprocessorObjectStyleMacroDefinition XYZ = (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0]; + IASTPreprocessorObjectStyleMacroDefinition defXYZ = (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0]; IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0]; IASTSimpleDeclSpecifier declSpec = (IASTSimpleDeclSpecifier) var.getDeclSpecifier(); IASTNodeLocation [] declSpecLocations = declSpec.getNodeLocations(); assertEquals( declSpecLocations.length, 2 ); IASTMacroExpansion expansion = (IASTMacroExpansion) declSpecLocations[0]; - assertEqualsMacros( XYZ, expansion.getMacroDefinition() ); + assertEqualsMacros( defXYZ, expansion.getMacroDefinition() ); assertEquals( expansion.getNodeOffset(), 0 ); - assertEquals( expansion.getNodeLength(), 6 ); + assertEquals( expansion.getNodeLength(), "const".length() ); IASTNodeLocation [] expansionLocations = expansion.getExpansionLocations(); assertEquals( expansionLocations.length, 1 ); assertTrue( expansionLocations[0] instanceof IASTFileLocation ); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java index 7f1f6309b95..1f08d060ee1 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2006 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 + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -189,6 +190,12 @@ public class DOMLocationTests extends AST2BaseTest { assertEquals(length, nodeLocation.getNodeLength()); } + private void assertFileLocation(IASTNode n, int offset, int length) { + IASTNodeLocation location = n.getFileLocation(); + assertEquals(offset, location.getNodeOffset()); + assertEquals(length, location.getNodeLength()); + } + public void testBug83664() throws Exception { String code = "int foo(x) int x; {\n return x;\n }\n"; //$NON-NLS-1$ IASTTranslationUnit tu = parse(code, ParserLanguage.C, true); @@ -449,7 +456,7 @@ public class DOMLocationTests extends AST2BaseTest { } } - public void _testBug162180() throws Exception { + public void testBug162180() throws Exception { StringBuffer buffer = new StringBuffer(); buffer.append( "#include \n"); //$NON-NLS-1$ int declOffset= buffer.length(); @@ -464,10 +471,127 @@ public class DOMLocationTests extends AST2BaseTest { assertEquals( statements.length, 0 ); IASTProblem[] problems = tu.getPreprocessorProblems(); assertEquals( problems.length, 1 ); - assertSoleLocation( decls[0], code.indexOf( "int x;"), "int x;".length() ); //$NON-NLS-1$ //$NON-NLS-2$ + assertSoleLocation( decls[0], code, "int x;"); } } - + + private void assertSoleLocation(IASTNode node, String code, String snip) { + assertSoleLocation(node, code.indexOf(snip), snip.length()); + } + + private void assertFileLocation(IASTNode node, String code, String snip) { + assertFileLocation(node, code.indexOf(snip), snip.length()); + } + + public void testBug162180_0() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "#include \n"); //$NON-NLS-1$ + buffer.append( "#include \r\n"); //$NON-NLS-1$ + buffer.append( "#include // more stuff \n"); //$NON-NLS-1$ + int declOffset= buffer.length(); + buffer.append( "int x;\n"); //$NON-NLS-1$ + String code = buffer.toString(); + for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP + : null) { + IASTTranslationUnit tu = parse(code, p, false, false); + IASTDeclaration[] decls= tu.getDeclarations(); + IASTPreprocessorStatement [] statements = tu.getAllPreprocessorStatements(); + IASTProblem[] problems = tu.getPreprocessorProblems(); + assertEquals( 1, decls.length); + assertEquals( 0, statements.length); + assertEquals( 3, problems.length); + String snip= ""; + assertSoleLocation(problems[0], code, "#include "); + assertSoleLocation(problems[1], code, "#include "); + assertSoleLocation(problems[2], code, "#include // more stuff "); + assertSoleLocation(decls[0], code, "int x;"); + } + } + + public void test162180_1() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "#define xxx(!) int a\n"); // [0-20] + buffer.append( "int x;\n"); // [21-27] + buffer.append( "int x\\i;\n"); // [28-36] + buffer.append( "int x2;\n"); // [37-44] + String code = buffer.toString(); + for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP + : null) { + IASTTranslationUnit tu = parse(code, p, false, false); + IASTDeclaration[] decls= tu.getDeclarations(); + IASTPreprocessorStatement [] statements = tu.getAllPreprocessorStatements(); + IASTProblem[] problems = tu.getPreprocessorProblems(); + assertEquals( 3, decls.length); + assertEquals( 0, statements.length); + assertEquals( 2, problems.length); + assertSoleLocation(problems[0], code, "xxx(!"); + assertSoleLocation( decls[0], code, "int x;"); + assertSoleLocation( problems[1], code, "\\"); + assertFileLocation( decls[1], code, "int x\\i"); + assertSoleLocation( decls[2], code, "int x2;"); + } + } + + public void test162180_2() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "#define ! x\n"); + buffer.append( "int x;\n"); + String code = buffer.toString(); + for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP + : null) { + IASTTranslationUnit tu = parse(code, p, false, false); + IASTDeclaration[] decls= tu.getDeclarations(); + IASTPreprocessorStatement [] statements = tu.getAllPreprocessorStatements(); + IASTProblem[] problems = tu.getPreprocessorProblems(); + assertEquals( 1, decls.length); + assertEquals( 0, statements.length); + assertEquals( 1, problems.length); + assertSoleLocation(problems[0], code, "!"); + assertSoleLocation( decls[0], code, "int x;"); + } + } + + public void test162180_3() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "#define nix(x) x\n"); + buffer.append( "nix(y,z);"); + buffer.append( "int x;\n"); + String code = buffer.toString(); + for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP + : null) { + IASTTranslationUnit tu = parse(code, p, false, false); + IASTDeclaration[] decls= tu.getDeclarations(); + IASTPreprocessorStatement [] statements = tu.getAllPreprocessorStatements(); + IASTProblem[] problems = tu.getPreprocessorProblems(); + assertEquals( 2, decls.length); + assertEquals( 1, statements.length); + assertEquals( 1, problems.length); + assertSoleLocation(problems[0], code, "z"); + assertSoleLocation( decls[1], code, "int x;"); + } + } + + public void test162180_4() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "#include \"\"\n"); + buffer.append( "#else\n"); + buffer.append( "int x;\n"); + String code = buffer.toString(); + for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP + : null) { + IASTTranslationUnit tu = parse(code, p, false, false); + IASTDeclaration[] decls= tu.getDeclarations(); + IASTPreprocessorStatement [] statements = tu.getAllPreprocessorStatements(); + IASTProblem[] problems = tu.getPreprocessorProblems(); + assertEquals( 1, decls.length); + assertEquals( 0, statements.length); + assertEquals( 2, problems.length); + assertSoleLocation(problems[0], code, "#include \"\""); + assertSoleLocation(problems[1], code, "else"); + assertSoleLocation( decls[0], code, "int x;"); + } + } + public void testBug85820() throws Exception { String code = "int *p = (int []){2, 4};"; //$NON-NLS-1$ IASTTranslationUnit tu = parse( code, ParserLanguage.C ); 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 41bcbe98cc7..68f0b4280c8 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2006 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 + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -16,7 +17,6 @@ import junit.framework.TestSuite; import org.eclipse.cdt.core.parser.tests.ParserTestSuite; import org.eclipse.cdt.core.parser.tests.prefix.CompletionTestSuite; -import org.eclipse.cdt.core.tests.FailingTest; /** * @author jcamelon @@ -32,7 +32,6 @@ public class DOMParserTestSuite extends TestCase { suite.addTestSuite( QuickParser2Tests.class ); suite.addTestSuite( CompleteParser2Tests.class ); suite.addTestSuite( DOMLocationTests.class ); - suite.addTest( new FailingTest(new DOMLocationTests("_testBug162180"), 162180) ); suite.addTestSuite( DOMLocationMacroTests.class ); suite.addTest( DOMLocationInclusionTests.suite() ); suite.addTestSuite( AST2KnRTests.class ); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java index 5e7197a5e77..89ab852da74 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java @@ -607,12 +607,11 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser { if (LA(1).hashCode() == checkOffset) failParseWithErrorHandling(); } catch (EndOfFileException e) { - // As expected - if (translationUnit.getDeclarations().length != 0) { - CASTNode d = (CASTNode) translationUnit.getDeclarations()[translationUnit - .getDeclarations().length - 1]; - ((CASTNode) translationUnit).setLength(d.getOffset() - + d.getLength()); + IASTDeclaration[] declarations = translationUnit.getDeclarations(); + // As expected + if (declarations.length != 0) { + CASTNode d = (CASTNode) declarations[declarations.length-1]; + ((CASTNode) translationUnit).setLength(d.getOffset() + d.getLength()); } else ((CASTNode) translationUnit).setLength(0); break; 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 df8d7f32325..604ee6a5f1d 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 @@ -90,16 +90,22 @@ abstract class BaseScanner implements IScanner { this.endOffset = end; this.macro = macro; } - - public final int startOffset; - - public final int endOffset; + + private final int startOffset; + private final int endOffset; public final IMacro macro; public String toString() { return macro.toString(); } + + public int getStartOffset() { + return startOffset; + } + public int getLength() { + return endOffset-startOffset; + } } protected ParserLanguage language; @@ -2110,17 +2116,19 @@ abstract class BaseScanner implements IScanner { } else if (expObject instanceof ObjectStyleMacro) { ObjectStyleMacro expMacro = (ObjectStyleMacro) expObject; char[] expText = expMacro.getExpansion(); - if (expText.length > 0) - pushContext(expText, new MacroData( - bufferPos[bufferStackPos] - expMacro.name.length + 1, - bufferPos[bufferStackPos], expMacro)); + if (expText.length > 0) { + final int endOffset= bufferPos[bufferStackPos]+1; + final int startOffset= endOffset - expMacro.name.length; + pushContext(expText, new MacroData(startOffset, endOffset, expMacro)); + } } else if (expObject instanceof DynamicStyleMacro) { DynamicStyleMacro expMacro = (DynamicStyleMacro) expObject; char[] expText = expMacro.execute(); - if (expText.length > 0) - pushContext(expText, new MacroData( - bufferPos[bufferStackPos] - expMacro.name.length - + 1, bufferPos[bufferStackPos], expMacro)); + if (expText.length > 0) { + final int endOffset= bufferPos[bufferStackPos]+1; + final int startOffset= endOffset - expMacro.name.length; + pushContext(expText, new MacroData(startOffset, endOffset, expMacro)); + } } else if (expObject instanceof char[]) { char[] expText = (char[]) expObject; @@ -2863,7 +2871,7 @@ abstract class BaseScanner implements IScanner { char[] fileNameArray = filename.toCharArray(); // TODO else we need to do macro processing on the rest of the line endLine = getLineNumber(bufferPos[bufferStackPos]); - skipToNewLine(); + skipToLastBeforeNewline(); findAndPushInclusion(filename, fileNameArray, local, include_next, startOffset, nameOffset, nameEndOffset, endOffset, startingLineNumber, nameLine, endLine); } @@ -3967,6 +3975,78 @@ abstract class BaseScanner implements IScanner { } } + /** + * Skips everything up to the next newline. Returns offset for last + * character that was not a whitespace and no comment. + * @since 4.0 + */ + protected int skipToLastBeforeNewline() { + char[] buffer = bufferStack[bufferStackPos]; + int limit = bufferLimit[bufferStackPos]; + int pos = bufferPos[bufferStackPos]; + int lastMeaningful= pos; + + boolean escaped = false; + boolean inComment= false; + while (++pos < limit) { + char ch= buffer[pos]; + switch (ch) { + case '/': + if (pos + 1 < limit) { + char c= buffer[pos + 1]; + if (c == '*') { + pos+=2; + while (++pos < limit) { + if (buffer[pos-1] == '*' && buffer[pos] == '/') { + pos++; + break; + } + } + pos--; + break; + } + else if (c == '/') { + inComment= true; + } + } + if (!inComment) { + lastMeaningful= pos; + } + break; + case '\\': + escaped = !escaped; + if (!inComment) { + lastMeaningful= pos; + } + continue; + case '\n': + if (escaped) { + escaped = false; + break; + } + bufferPos[bufferStackPos]= pos-1; + return lastMeaningful; + case '\r': + if (escaped) { + escaped = false; + break; + } else if (pos+1 < limit && buffer[pos+1] == '\n') { + bufferPos[bufferStackPos]= pos-1; + return lastMeaningful; + } + break; + default: + if (!inComment && !Character.isWhitespace(ch)) { + lastMeaningful= pos; + } + break; + } + escaped = false; + } + bufferPos[bufferStackPos]= pos-1; + return lastMeaningful; + } + protected char[] handleFunctionStyleMacro(FunctionStyleMacro macro, boolean pushContext) { char[] buffer = bufferStack[bufferStackPos]; @@ -4133,8 +4213,7 @@ abstract class BaseScanner implements IScanner { } if (pushContext) { - pushContext(result, new MacroData(start, bufferPos[bufferStackPos], - macro)); + pushContext(result, new MacroData(start, bufferPos[bufferStackPos]+1, macro)); } return result; } @@ -4583,13 +4662,13 @@ abstract class BaseScanner implements IScanner { // standard built-ins protected static final ObjectStyleMacro __cplusplus = new ObjectStyleMacro( - "__cplusplus".toCharArray(), ONE); //$NON-NLS-1$ //$NON-NLS-2$ + "__cplusplus".toCharArray(), ONE); //$NON-NLS-1$ protected static final ObjectStyleMacro __STDC__ = new ObjectStyleMacro( - "__STDC__".toCharArray(), ONE); //$NON-NLS-1$ //$NON-NLS-2$ + "__STDC__".toCharArray(), ONE); //$NON-NLS-1$ protected static final ObjectStyleMacro __STDC_HOSTED__ = new ObjectStyleMacro( - "__STDC_HOSTED_".toCharArray(), ONE); //$NON-NLS-1$ //$NON-NLS-2$ + "__STDC_HOSTED_".toCharArray(), ONE); //$NON-NLS-1$ protected static final ObjectStyleMacro __STDC_VERSION__ = new ObjectStyleMacro( "__STDC_VERSION_".toCharArray(), "199901L".toCharArray()); //$NON-NLS-1$ //$NON-NLS-2$ @@ -4780,107 +4859,107 @@ abstract class BaseScanner implements IScanner { CharArrayIntMap words = new CharArrayIntMap(IToken.tLAST, -1); // Common keywords - words.put(Keywords.cAUTO, IToken.t_auto); //$NON-NLS-1$ - words.put(Keywords.cBREAK, IToken.t_break); //$NON-NLS-1$ - words.put(Keywords.cCASE, IToken.t_case); //$NON-NLS-1$ - words.put(Keywords.cCHAR, IToken.t_char); //$NON-NLS-1$ - words.put(Keywords.cCONST, IToken.t_const); //$NON-NLS-1$ - words.put(Keywords.cCONTINUE, IToken.t_continue); //$NON-NLS-1$ - words.put(Keywords.cDEFAULT, IToken.t_default); //$NON-NLS-1$ - words.put(Keywords.cDO, IToken.t_do); //$NON-NLS-1$ - words.put(Keywords.cDOUBLE, IToken.t_double); //$NON-NLS-1$ - words.put(Keywords.cELSE, IToken.t_else); //$NON-NLS-1$ - words.put(Keywords.cENUM, IToken.t_enum); //$NON-NLS-1$ - words.put(Keywords.cEXTERN, IToken.t_extern); //$NON-NLS-1$ - words.put(Keywords.cFLOAT, IToken.t_float); //$NON-NLS-1$ - words.put(Keywords.cFOR, IToken.t_for); //$NON-NLS-1$ - words.put(Keywords.cGOTO, IToken.t_goto); //$NON-NLS-1$ - words.put(Keywords.cIF, IToken.t_if); //$NON-NLS-1$ - words.put(Keywords.cINLINE, IToken.t_inline); //$NON-NLS-1$ - words.put(Keywords.cINT, IToken.t_int); //$NON-NLS-1$ - words.put(Keywords.cLONG, IToken.t_long); //$NON-NLS-1$ - words.put(Keywords.cREGISTER, IToken.t_register); //$NON-NLS-1$ - words.put(Keywords.cRETURN, IToken.t_return); //$NON-NLS-1$ - words.put(Keywords.cSHORT, IToken.t_short); //$NON-NLS-1$ - words.put(Keywords.cSIGNED, IToken.t_signed); //$NON-NLS-1$ - words.put(Keywords.cSIZEOF, IToken.t_sizeof); //$NON-NLS-1$ - words.put(Keywords.cSTATIC, IToken.t_static); //$NON-NLS-1$ - words.put(Keywords.cSTRUCT, IToken.t_struct); //$NON-NLS-1$ - words.put(Keywords.cSWITCH, IToken.t_switch); //$NON-NLS-1$ - words.put(Keywords.cTYPEDEF, IToken.t_typedef); //$NON-NLS-1$ - words.put(Keywords.cUNION, IToken.t_union); //$NON-NLS-1$ - words.put(Keywords.cUNSIGNED, IToken.t_unsigned); //$NON-NLS-1$ - words.put(Keywords.cVOID, IToken.t_void); //$NON-NLS-1$ - words.put(Keywords.cVOLATILE, IToken.t_volatile); //$NON-NLS-1$ - words.put(Keywords.cWHILE, IToken.t_while); //$NON-NLS-1$ - words.put(Keywords.cASM, IToken.t_asm); //$NON-NLS-1$ + words.put(Keywords.cAUTO, IToken.t_auto); + words.put(Keywords.cBREAK, IToken.t_break); + words.put(Keywords.cCASE, IToken.t_case); + words.put(Keywords.cCHAR, IToken.t_char); + words.put(Keywords.cCONST, IToken.t_const); + words.put(Keywords.cCONTINUE, IToken.t_continue); + words.put(Keywords.cDEFAULT, IToken.t_default); + words.put(Keywords.cDO, IToken.t_do); + words.put(Keywords.cDOUBLE, IToken.t_double); + words.put(Keywords.cELSE, IToken.t_else); + words.put(Keywords.cENUM, IToken.t_enum); + words.put(Keywords.cEXTERN, IToken.t_extern); + words.put(Keywords.cFLOAT, IToken.t_float); + words.put(Keywords.cFOR, IToken.t_for); + words.put(Keywords.cGOTO, IToken.t_goto); + words.put(Keywords.cIF, IToken.t_if); + words.put(Keywords.cINLINE, IToken.t_inline); + words.put(Keywords.cINT, IToken.t_int); + words.put(Keywords.cLONG, IToken.t_long); + words.put(Keywords.cREGISTER, IToken.t_register); + words.put(Keywords.cRETURN, IToken.t_return); + words.put(Keywords.cSHORT, IToken.t_short); + words.put(Keywords.cSIGNED, IToken.t_signed); + words.put(Keywords.cSIZEOF, IToken.t_sizeof); + words.put(Keywords.cSTATIC, IToken.t_static); + words.put(Keywords.cSTRUCT, IToken.t_struct); + words.put(Keywords.cSWITCH, IToken.t_switch); + words.put(Keywords.cTYPEDEF, IToken.t_typedef); + words.put(Keywords.cUNION, IToken.t_union); + words.put(Keywords.cUNSIGNED, IToken.t_unsigned); + words.put(Keywords.cVOID, IToken.t_void); + words.put(Keywords.cVOLATILE, IToken.t_volatile); + words.put(Keywords.cWHILE, IToken.t_while); + words.put(Keywords.cASM, IToken.t_asm); // ANSI C keywords ckeywords = (CharArrayIntMap) words.clone(); - ckeywords.put(Keywords.cRESTRICT, IToken.t_restrict); //$NON-NLS-1$ - ckeywords.put(Keywords.c_BOOL, IToken.t__Bool); //$NON-NLS-1$ - ckeywords.put(Keywords.c_COMPLEX, IToken.t__Complex); //$NON-NLS-1$ - ckeywords.put(Keywords.c_IMAGINARY, IToken.t__Imaginary); //$NON-NLS-1$ + ckeywords.put(Keywords.cRESTRICT, IToken.t_restrict); + ckeywords.put(Keywords.c_BOOL, IToken.t__Bool); + ckeywords.put(Keywords.c_COMPLEX, IToken.t__Complex); + ckeywords.put(Keywords.c_IMAGINARY, IToken.t__Imaginary); // C++ Keywords cppkeywords = words; - cppkeywords.put(Keywords.cBOOL, IToken.t_bool); //$NON-NLS-1$ - cppkeywords.put(Keywords.cCATCH, IToken.t_catch); //$NON-NLS-1$ - cppkeywords.put(Keywords.cCLASS, IToken.t_class); //$NON-NLS-1$ - cppkeywords.put(Keywords.cCONST_CAST, IToken.t_const_cast); //$NON-NLS-1$ - cppkeywords.put(Keywords.cDELETE, IToken.t_delete); //$NON-NLS-1$ - cppkeywords.put(Keywords.cDYNAMIC_CAST, IToken.t_dynamic_cast); //$NON-NLS-1$ - cppkeywords.put(Keywords.cEXPLICIT, IToken.t_explicit); //$NON-NLS-1$ - cppkeywords.put(Keywords.cEXPORT, IToken.t_export); //$NON-NLS-1$ - cppkeywords.put(Keywords.cFALSE, IToken.t_false); //$NON-NLS-1$ - cppkeywords.put(Keywords.cFRIEND, IToken.t_friend); //$NON-NLS-1$ - cppkeywords.put(Keywords.cMUTABLE, IToken.t_mutable); //$NON-NLS-1$ - cppkeywords.put(Keywords.cNAMESPACE, IToken.t_namespace); //$NON-NLS-1$ - cppkeywords.put(Keywords.cNEW, IToken.t_new); //$NON-NLS-1$ - cppkeywords.put(Keywords.cOPERATOR, IToken.t_operator); //$NON-NLS-1$ - cppkeywords.put(Keywords.cPRIVATE, IToken.t_private); //$NON-NLS-1$ - cppkeywords.put(Keywords.cPROTECTED, IToken.t_protected); //$NON-NLS-1$ - cppkeywords.put(Keywords.cPUBLIC, IToken.t_public); //$NON-NLS-1$ - cppkeywords.put(Keywords.cREINTERPRET_CAST, IToken.t_reinterpret_cast); //$NON-NLS-1$ - cppkeywords.put(Keywords.cSTATIC_CAST, IToken.t_static_cast); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTEMPLATE, IToken.t_template); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTHIS, IToken.t_this); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTHROW, IToken.t_throw); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTRUE, IToken.t_true); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTRY, IToken.t_try); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTYPEID, IToken.t_typeid); //$NON-NLS-1$ - cppkeywords.put(Keywords.cTYPENAME, IToken.t_typename); //$NON-NLS-1$ - cppkeywords.put(Keywords.cUSING, IToken.t_using); //$NON-NLS-1$ - cppkeywords.put(Keywords.cVIRTUAL, IToken.t_virtual); //$NON-NLS-1$ - cppkeywords.put(Keywords.cWCHAR_T, IToken.t_wchar_t); //$NON-NLS-1$ + cppkeywords.put(Keywords.cBOOL, IToken.t_bool); + cppkeywords.put(Keywords.cCATCH, IToken.t_catch); + cppkeywords.put(Keywords.cCLASS, IToken.t_class); + cppkeywords.put(Keywords.cCONST_CAST, IToken.t_const_cast); + cppkeywords.put(Keywords.cDELETE, IToken.t_delete); + cppkeywords.put(Keywords.cDYNAMIC_CAST, IToken.t_dynamic_cast); + cppkeywords.put(Keywords.cEXPLICIT, IToken.t_explicit); + cppkeywords.put(Keywords.cEXPORT, IToken.t_export); + cppkeywords.put(Keywords.cFALSE, IToken.t_false); + cppkeywords.put(Keywords.cFRIEND, IToken.t_friend); + cppkeywords.put(Keywords.cMUTABLE, IToken.t_mutable); + cppkeywords.put(Keywords.cNAMESPACE, IToken.t_namespace); + cppkeywords.put(Keywords.cNEW, IToken.t_new); + cppkeywords.put(Keywords.cOPERATOR, IToken.t_operator); + cppkeywords.put(Keywords.cPRIVATE, IToken.t_private); + cppkeywords.put(Keywords.cPROTECTED, IToken.t_protected); + cppkeywords.put(Keywords.cPUBLIC, IToken.t_public); + cppkeywords.put(Keywords.cREINTERPRET_CAST, IToken.t_reinterpret_cast); + cppkeywords.put(Keywords.cSTATIC_CAST, IToken.t_static_cast); + cppkeywords.put(Keywords.cTEMPLATE, IToken.t_template); + cppkeywords.put(Keywords.cTHIS, IToken.t_this); + cppkeywords.put(Keywords.cTHROW, IToken.t_throw); + cppkeywords.put(Keywords.cTRUE, IToken.t_true); + cppkeywords.put(Keywords.cTRY, IToken.t_try); + cppkeywords.put(Keywords.cTYPEID, IToken.t_typeid); + cppkeywords.put(Keywords.cTYPENAME, IToken.t_typename); + cppkeywords.put(Keywords.cUSING, IToken.t_using); + cppkeywords.put(Keywords.cVIRTUAL, IToken.t_virtual); + cppkeywords.put(Keywords.cWCHAR_T, IToken.t_wchar_t); // C++ operator alternative - cppkeywords.put(Keywords.cAND, IToken.t_and); //$NON-NLS-1$ - cppkeywords.put(Keywords.cAND_EQ, IToken.t_and_eq); //$NON-NLS-1$ - cppkeywords.put(Keywords.cBITAND, IToken.t_bitand); //$NON-NLS-1$ - cppkeywords.put(Keywords.cBITOR, IToken.t_bitor); //$NON-NLS-1$ - cppkeywords.put(Keywords.cCOMPL, IToken.t_compl); //$NON-NLS-1$ - cppkeywords.put(Keywords.cNOT, IToken.t_not); //$NON-NLS-1$ - cppkeywords.put(Keywords.cNOT_EQ, IToken.t_not_eq); //$NON-NLS-1$ - cppkeywords.put(Keywords.cOR, IToken.t_or); //$NON-NLS-1$ - cppkeywords.put(Keywords.cOR_EQ, IToken.t_or_eq); //$NON-NLS-1$ - cppkeywords.put(Keywords.cXOR, IToken.t_xor); //$NON-NLS-1$ - cppkeywords.put(Keywords.cXOR_EQ, IToken.t_xor_eq); //$NON-NLS-1$ + cppkeywords.put(Keywords.cAND, IToken.t_and); + cppkeywords.put(Keywords.cAND_EQ, IToken.t_and_eq); + cppkeywords.put(Keywords.cBITAND, IToken.t_bitand); + cppkeywords.put(Keywords.cBITOR, IToken.t_bitor); + cppkeywords.put(Keywords.cCOMPL, IToken.t_compl); + cppkeywords.put(Keywords.cNOT, IToken.t_not); + cppkeywords.put(Keywords.cNOT_EQ, IToken.t_not_eq); + cppkeywords.put(Keywords.cOR, IToken.t_or); + cppkeywords.put(Keywords.cOR_EQ, IToken.t_or_eq); + cppkeywords.put(Keywords.cXOR, IToken.t_xor); + cppkeywords.put(Keywords.cXOR_EQ, IToken.t_xor_eq); // Preprocessor keywords ppKeywords = new CharArrayIntMap(16, -1); - ppKeywords.put(Keywords.cIF, ppIf); //$NON-NLS-1$ - ppKeywords.put(Keywords.cIFDEF, ppIfdef); //$NON-NLS-1$ - ppKeywords.put(Keywords.cIFNDEF, ppIfndef); //$NON-NLS-1$ - ppKeywords.put(Keywords.cELIF, ppElif); //$NON-NLS-1$ - ppKeywords.put(Keywords.cELSE, ppElse); //$NON-NLS-1$ - ppKeywords.put(Keywords.cENDIF, ppEndif); //$NON-NLS-1$ - ppKeywords.put(Keywords.cINCLUDE, ppInclude); //$NON-NLS-1$ - ppKeywords.put(Keywords.cDEFINE, ppDefine); //$NON-NLS-1$ - ppKeywords.put(Keywords.cUNDEF, ppUndef); //$NON-NLS-1$ - ppKeywords.put(Keywords.cERROR, ppError); //$NON-NLS-1$ - ppKeywords.put(Keywords.cINCLUDE_NEXT, ppInclude_next); //$NON-NLS-1$ - ppKeywords.put(Keywords.cPRAGMA, ppPragma); //$NON-NLS-1$ + ppKeywords.put(Keywords.cIF, ppIf); + ppKeywords.put(Keywords.cIFDEF, ppIfdef); + ppKeywords.put(Keywords.cIFNDEF, ppIfndef); + ppKeywords.put(Keywords.cELIF, ppElif); + ppKeywords.put(Keywords.cELSE, ppElse); + ppKeywords.put(Keywords.cENDIF, ppEndif); + ppKeywords.put(Keywords.cINCLUDE, ppInclude); + ppKeywords.put(Keywords.cDEFINE, ppDefine); + ppKeywords.put(Keywords.cUNDEF, ppUndef); + ppKeywords.put(Keywords.cERROR, ppError); + ppKeywords.put(Keywords.cINCLUDE_NEXT, ppInclude_next); + ppKeywords.put(Keywords.cPRAGMA, ppPragma); } /** 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 ea35dae56c0..21d5349d138 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 @@ -133,7 +133,7 @@ public class DOMScanner extends BaseScanner { char[] filenamePath, boolean local, int startOffset, int startingLineNumber, int nameOffset, int nameEndOffset, int nameLine, int endOffset, int endLine, boolean isForced) { - return new DOMInclusion(filenamePath, resolveOffset(startOffset)); + return new DOMInclusion(filenamePath, getGlobalOffset(startOffset)); } /* @@ -148,13 +148,13 @@ public class DOMScanner extends BaseScanner { IScannerPreprocessorLog.IMacroDefinition m = null; if (macro instanceof FunctionStyleMacro) m = locationMap.defineFunctionStyleMacro( - (FunctionStyleMacro) macro, resolveOffset(startingOffset), - resolveOffset(idstart), resolveOffset(idend), - resolveOffset(textEnd)); + (FunctionStyleMacro) macro, getGlobalOffset(startingOffset), + getGlobalOffset(idstart), getGlobalOffset(idend), + getGlobalOffset(textEnd)); else if (macro instanceof ObjectStyleMacro) m = locationMap.defineObjectStyleMacro((ObjectStyleMacro) macro, - resolveOffset(startingOffset), resolveOffset(idstart), - resolveOffset(idend), resolveOffset(textEnd)); + getGlobalOffset(startingOffset), getGlobalOffset(idstart), + getGlobalOffset(idend), getGlobalOffset(textEnd)); if (m != null && macro instanceof ObjectStyleMacro) ((ObjectStyleMacro) macro).attachment = m; @@ -194,7 +194,7 @@ public class DOMScanner extends BaseScanner { if( ! isCircularInclusion( (InclusionData) data )) { DOMInclusion inc = ((DOMInclusion) ((InclusionData) data).inclusion); - locationMap.startInclusion(((InclusionData) data).reader, inc.o, resolveOffset(getCurrentOffset())); + locationMap.startInclusion(((InclusionData) data).reader, inc.o, getGlobalOffset(getCurrentOffset())+1); bufferDelta[bufferStackPos + 1] = 0; } } @@ -203,21 +203,23 @@ public class DOMScanner extends BaseScanner { MacroData d = (MacroData) data; if (d.macro instanceof FunctionStyleMacro && fsmCount == 0) { FunctionStyleMacro fsm = (FunctionStyleMacro) d.macro; + int startOffset= getGlobalOffset(d.getStartOffset()); + int endOffset= startOffset+d.getLength(); locationMap.startFunctionStyleExpansion(fsm.attachment, - fsm.arglist, resolveOffset(d.startOffset), - resolveOffset(d.endOffset)); + fsm.arglist, startOffset, endOffset); bufferDelta[bufferStackPos + 1] = 0; } else if (d.macro instanceof ObjectStyleMacro && fsmCount == 0) { ObjectStyleMacro osm = (ObjectStyleMacro) d.macro; + int startOffset= getGlobalOffset(d.getStartOffset()); + int endOffset= startOffset+d.getLength(); locationMap.startObjectStyleMacroExpansion(osm.attachment, - resolveOffset(d.startOffset), - resolveOffset(d.endOffset)); + startOffset, endOffset); bufferDelta[bufferStackPos + 1] = 0; } } else if( data instanceof CodeReader && !macroFilesInitialized ) { - int resolved = getGlobalCounter(0); + int resolved = getGlobalOffset(0, 0); locationMap.startInclusion( (CodeReader) data, resolved, resolved ); } @@ -233,23 +235,21 @@ public class DOMScanner extends BaseScanner { */ protected Object popContext() { Object result = super.popContext(); - int delta_pos = 0; - if( bufferPos[bufferStackPos + 1] > bufferLimit[ bufferStackPos + 1 ] ) - delta_pos += bufferLimit[ bufferStackPos + 1 ]; - else - delta_pos += bufferPos[ bufferStackPos + 1 ]; + int bufferEndOffset = Math.min(bufferPos[bufferStackPos+1] + 1, bufferLimit[bufferStackPos+1]); if (result instanceof CodeReader) { - if( isInitialized ) - locationMap.endTranslationUnit(bufferDelta[0] - + ((CodeReader) result).buffer.length); - else - { - bufferDelta[0] += bufferDelta[bufferStackPos + 1] + ((CodeReader) result).buffer.length; - locationMap.endInclusion( ((CodeReader) result), getGlobalCounter(0) ); + CodeReader codeReader = (CodeReader) result; + if( isInitialized ) { + locationMap.endTranslationUnit(bufferDelta[0] + codeReader.buffer.length); + } + else { + // handling of macro-file + bufferDelta[0] += codeReader.buffer.length; + locationMap.endInclusion(codeReader, getGlobalOffset(0,0)); } - } else if (result instanceof InclusionData) { + } + else if (result instanceof InclusionData) { CodeReader codeReader = ((InclusionData) result).reader; if (log.isTracing()) { StringBuffer buffer = new StringBuffer("Exiting inclusion "); //$NON-NLS-1$ @@ -257,64 +257,33 @@ public class DOMScanner extends BaseScanner { log.traceLog(buffer.toString()); } - int value = getGlobalCounter(bufferStackPos + 1) + delta_pos; - locationMap.endInclusion(codeReader, value); - bufferDelta[bufferStackPos] += bufferDelta[bufferStackPos + 1] - + codeReader.buffer.length; - } else if (result instanceof MacroData) { + int endOffset = getGlobalOffset(bufferStackPos+1, bufferEndOffset); + locationMap.endInclusion(codeReader, endOffset); + bufferDelta[bufferStackPos] += bufferDelta[bufferStackPos + 1] + bufferEndOffset; + } + else if (result instanceof MacroData) { + int endOffset = getGlobalOffset(bufferStackPos + 1, bufferEndOffset); MacroData data = (MacroData) result; if (data.macro instanceof FunctionStyleMacro && fsmCount == 0) { - - locationMap - .endFunctionStyleExpansion(((FunctionStyleMacro)data.macro).attachment, getGlobalCounter(bufferStackPos + 1) - + delta_pos + 1); // functionstyle - // macro) - // ; - bufferDelta[bufferStackPos] += bufferDelta[bufferStackPos + 1] - + delta_pos + 1; - } else if (data.macro instanceof ObjectStyleMacro && fsmCount == 0) { - locationMap - .endObjectStyleMacroExpansion(((ObjectStyleMacro)data.macro).attachment, getGlobalCounter(bufferStackPos + 1) - + delta_pos ); - bufferDelta[bufferStackPos] += bufferDelta[bufferStackPos + 1] - + delta_pos; - + locationMap.endFunctionStyleExpansion(((FunctionStyleMacro)data.macro).attachment, endOffset); + bufferDelta[bufferStackPos]+= bufferDelta[bufferStackPos + 1] + bufferEndOffset; + } + else if (data.macro instanceof ObjectStyleMacro && fsmCount == 0) { + locationMap.endObjectStyleMacroExpansion(((ObjectStyleMacro)data.macro).attachment, endOffset); + bufferDelta[bufferStackPos]+= bufferDelta[bufferStackPos + 1] + bufferEndOffset; } } return result; } - protected int getGlobalCounter(int value) { - if (value < 0) - return 0; - int result = bufferDelta[value]; - for (int i = value - 1; i >= 0; --i) - { - if( bufferPos[i] > bufferLimit[i] ) - result += bufferLimit[i] + bufferDelta[i]; - else - result += bufferPos[i] + bufferDelta[i]; - } - - return result; - - } - - protected int getGlobalCounter() { - return getGlobalCounter(bufferStackPos); - } - - /** - * @return - */ protected IToken newToken(int signal) { return new _BasicToken(signal, - resolveOffset(bufferPos[bufferStackPos] + 1)); + getGlobalOffset(bufferPos[bufferStackPos] + 1)); } protected IToken newToken(int signal, char[] buffer) { IToken i = new _ImagedToken(signal, buffer, - resolveOffset(bufferPos[bufferStackPos] + 1)); + getGlobalOffset(bufferPos[bufferStackPos] + 1)); if (buffer != null && buffer.length == 0 && signal != IToken.tSTRING && signal != IToken.tLSTRING) bufferPos[bufferStackPos] += 1; // TODO - remove this hack at some @@ -340,18 +309,27 @@ public class DOMScanner extends BaseScanner { */ protected void handleProblem(int id, int offset, char[] arg) { IASTProblem problem = new ScannerASTProblem(id, arg, true, false); - int o = resolveOffset(offset); + int o = getGlobalOffset(offset); ((ScannerASTProblem) problem).setOffsetAndLength(o, - resolveOffset(getCurrentOffset() + 1) - o); + getGlobalOffset(getCurrentOffset() + 1) - o); locationMap.encounterProblem(problem); } /** - * @param offset - * @return + * works only if bufferOffset is past the last context inserted in the current one. */ - private int resolveOffset(int offset) { - return getGlobalCounter() + offset; + private int getGlobalOffset(int bufferOffset) { + return getGlobalOffset(bufferStackPos, bufferOffset); + } + + private int getGlobalOffset(int stackPos, int offset) { + if (stackPos < 0) + return offset; + offset+= bufferDelta[stackPos]; + for (int i = stackPos - 1; i >= 0; --i) { + offset+= Math.min(bufferPos[i]+1, bufferLimit[i]) + bufferDelta[i]; + } + return offset; } /* @@ -375,11 +353,11 @@ public class DOMScanner extends BaseScanner { protected void processIfdef(int startPos, int endPos, boolean positive, boolean taken) { if (positive) - locationMap.encounterPoundIfdef(resolveOffset(startPos), - resolveOffset(endPos), taken); + locationMap.encounterPoundIfdef(getGlobalOffset(startPos), + getGlobalOffset(endPos), taken); else - locationMap.encounterPoundIfndef(resolveOffset(startPos), - resolveOffset(endPos), taken); + locationMap.encounterPoundIfndef(getGlobalOffset(startPos), + getGlobalOffset(endPos), taken); } @@ -390,8 +368,8 @@ public class DOMScanner extends BaseScanner { * int, boolean) */ protected void processIf(int startPos, int endPos, boolean taken) { - locationMap.encounterPoundIf(resolveOffset(startPos), - resolveOffset(endPos), taken); + locationMap.encounterPoundIf(getGlobalOffset(startPos), + getGlobalOffset(endPos), taken); } /* @@ -401,8 +379,8 @@ public class DOMScanner extends BaseScanner { * int, boolean) */ protected void processElsif(int startPos, int endPos, boolean taken) { - locationMap.encounterPoundElif(resolveOffset(startPos), - resolveOffset(endPos), taken); + locationMap.encounterPoundElif(getGlobalOffset(startPos), + getGlobalOffset(endPos), taken); } /* @@ -412,8 +390,8 @@ public class DOMScanner extends BaseScanner { * int, boolean) */ protected void processElse(int startPos, int endPos, boolean taken) { - locationMap.encounterPoundElse(resolveOffset(startPos), - resolveOffset(endPos), taken); + locationMap.encounterPoundElse(getGlobalOffset(startPos), + getGlobalOffset(endPos), taken); } /* @@ -426,8 +404,8 @@ public class DOMScanner extends BaseScanner { int namePos, Object definition) { final IScannerPreprocessorLog.IMacroDefinition macroDefinition = (definition instanceof ObjectStyleMacro) ? ((ObjectStyleMacro) definition).attachment : null; - locationMap.encounterPoundUndef(resolveOffset(pos), - resolveOffset(endPos), symbol, namePos, macroDefinition); + locationMap.encounterPoundUndef(getGlobalOffset(pos), + getGlobalOffset(endPos), symbol, namePos, macroDefinition); } /* @@ -437,8 +415,8 @@ public class DOMScanner extends BaseScanner { * int) */ protected void processError(int startPos, int endPos) { - locationMap.encounterPoundError(resolveOffset(startPos), - resolveOffset(endPos)); + locationMap.encounterPoundError(getGlobalOffset(startPos), + getGlobalOffset(endPos)); } /* @@ -448,8 +426,7 @@ public class DOMScanner extends BaseScanner { * int) */ protected void processEndif(int startPos, int endPos) { - locationMap.encounterPoundEndIf(resolveOffset(startPos), - resolveOffset(endPos)); + locationMap.encounterPoundEndIf(getGlobalOffset(startPos), getGlobalOffset(endPos)); } /* @@ -459,8 +436,7 @@ public class DOMScanner extends BaseScanner { * int) */ protected void processPragma(int startPos, int endPos) { - locationMap.encounterPoundPragma(resolveOffset(startPos), - resolveOffset(endPos)); + locationMap.encounterPoundPragma(getGlobalOffset(startPos), getGlobalOffset(endPos)); } protected void beforeReplaceAllMacros() { 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 fd0a498f13f..01af7463591 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 @@ -913,26 +913,50 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { this.parent = parent; } + /** + * Global offset of start of directive (inclusive). + */ public int context_directive_start; - public int context_directive_end; // inclusive - public int context_ends; // inclusive + /** + * Global offset of end of directive (exclusive). + */ + public int context_directive_end; + /** + * Global offset of end of context introduced by directive (exclusive). + */ + public int context_ends; public _CompositeContext parent; public _CompositeContext getParent() { return parent; } + public int getDirectiveStart() { + return context_directive_start; + } + + public int getDirectiveLength() { + return context_directive_end - context_directive_start; + } + + public int getContextStart() { + return context_directive_end; + } + + public int getContextLength() { + return context_ends- context_directive_end; + } + public final boolean contains(int offset) { - return (offset >= context_directive_start && offset <= context_ends); + return (offset >= context_directive_start && offset < context_ends); + } + + public final boolean containsInContext(int offset) { + return (offset >= context_directive_end && offset < context_ends); } public boolean containsInDirective(int offset, int length) { - if( length > 0 && offset == context_directive_end ) - return false; - if (offset >= context_directive_start - && offset + length - 1 <= context_directive_end ) - return true; - return false; + return context_directive_start <= offset && offset + length <= context_directive_end; } public boolean hasAncestor(_Context cc) { @@ -1005,7 +1029,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { if (c== null || offset < c.context_directive_start) { right= middle-1; } - else if (offset > c.context_ends) { + else if (offset >= c.context_ends) { left= middle+1; } else { @@ -1021,7 +1045,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } } // if no sub context contains this, do it this way - if ((offset >= context_directive_start && offset <= context_ends)) + if ((offset >= context_directive_start && offset < context_ends)) return this; return null; } @@ -1037,6 +1061,23 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { return result; } + public int getOffsetInContext(int globalOffset) { + int externalCount= 0; + if (subContexts != null) { + for (int i = 0; i < subContexts.length; ++i) { + _Context subC = subContexts[i]; + if (subC == null || globalOffset < subC.getContextStart()) { + break; + } + if (subC instanceof _CompositeContext) { + assert !subC.containsInContext(globalOffset); + externalCount+= subC.getContextLength(); + } + } + } + final int result = globalOffset - externalCount - getContextStart(); + return ((result < 0) ? 0 : result); + } } protected static class _CompositeFileContext extends _CompositeContext { @@ -1065,7 +1106,6 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } return lineNumber; } - } protected static class _Inclusion extends _CompositeFileContext implements @@ -1079,10 +1119,6 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { protected static class _TranslationUnit extends _CompositeFileContext { - /** - * @param startOffset - * @param endOffset - */ public _TranslationUnit() { super(null, 0, 0 ); } @@ -1248,7 +1284,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { expansionName = new ASTMacroName( definition.getName() ); expansionName.setParent( rootNode ); expansionName.setPropertyInParent( IASTTranslationUnit.EXPANSION_NAME ); - ((ASTNode)expansionName).setOffsetAndLength( context_directive_start, context_directive_end - context_directive_start + 1); + ((ASTNode)expansionName).setOffsetAndLength(context_directive_start, getDirectiveLength()); } return expansionName; } @@ -1358,8 +1394,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { ((ASTNode) name).setOffsetAndLength(d.nameOffset, d.name.length); r.setName(name); r.setExpansion(new String(d.getExpansion())); - ((ASTNode) r).setOffsetAndLength(d.context_directive_start, - d.context_directive_end - d.context_directive_start); + ((ASTNode) r).setOffsetAndLength(d.context_directive_start, d.getDirectiveLength()); d.astNode = r; r.setParent(rootNode); r.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); @@ -1392,11 +1427,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { private IASTPreprocessorIncludeStatement createASTInclusion(_Inclusion inc) { IASTPreprocessorIncludeStatement result = new ASTInclusionStatement( inc.reader.filename); - ((ASTNode) result).setOffsetAndLength( - inc.context_directive_start, inc.context_directive_end - - inc.context_directive_start); - ((ASTInclusionStatement) result).startOffset = inc.context_directive_end; - ((ASTInclusionStatement) result).endOffset = inc.context_ends; + ((ASTNode) result).setOffsetAndLength(inc.context_directive_start, inc.getDirectiveLength()); + ((ASTInclusionStatement) result).startOffset = inc.getContextStart(); + ((ASTInclusionStatement) result).endOffset = inc.context_directive_end; ((ASTInclusionStatement) result).setParent(rootNode); ((ASTInclusionStatement) result).setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); result.setParent(rootNode); @@ -1457,8 +1490,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTEndif(_Endif endif) { IASTPreprocessorEndifStatement result = new ASTEndif(); - ((ASTNode) result).setOffsetAndLength(endif.context_directive_start, - endif.context_directive_end - endif.context_directive_start); + ((ASTNode) result).setOffsetAndLength(endif.context_directive_start, endif.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1470,8 +1502,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTElif(_Elif elif) { IASTPreprocessorElifStatement result = new ASTElif(elif.taken); - ((ASTNode) result).setOffsetAndLength(elif.context_directive_start, - elif.context_directive_end - elif.context_directive_start); + ((ASTNode) result).setOffsetAndLength(elif.context_directive_start, elif.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1483,8 +1514,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTElse(_Else e) { IASTPreprocessorElseStatement result = new ASTElse(e.taken); - ((ASTNode) result).setOffsetAndLength(e.context_directive_start, - e.context_directive_end - e.context_directive_start); + ((ASTNode) result).setOffsetAndLength(e.context_directive_start, e.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1496,8 +1526,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTIfndef(_Ifndef ifndef) { IASTPreprocessorIfndefStatement result = new ASTIfndef(ifndef.taken); - ((ASTNode) result).setOffsetAndLength(ifndef.context_directive_start, - ifndef.context_directive_end - ifndef.context_directive_start); + ((ASTNode) result).setOffsetAndLength(ifndef.context_directive_start, ifndef.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1509,8 +1538,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTIfdef(_Ifdef ifdef) { IASTPreprocessorIfdefStatement result = new ASTIfdef(ifdef.taken); - ((ASTNode) result).setOffsetAndLength(ifdef.context_directive_start, - ifdef.context_directive_end - ifdef.context_directive_start); + ((ASTNode) result).setOffsetAndLength(ifdef.context_directive_start, ifdef.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1522,8 +1550,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTIf(_If i) { IASTPreprocessorIfStatement result = new ASTIf(i.taken); - ((ASTNode) result).setOffsetAndLength(i.context_directive_start, - i.context_directive_end - i.context_directive_start); + ((ASTNode) result).setOffsetAndLength(i.context_directive_start, i.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1535,8 +1562,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTError(_Error error) { IASTPreprocessorErrorStatement result = new ASTError(); - ((ASTNode) result).setOffsetAndLength(error.context_directive_start, - error.context_directive_end - error.context_directive_start); + ((ASTNode) result).setOffsetAndLength(error.context_directive_start, error.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1548,8 +1574,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTPragma(_Pragma pragma) { IASTPreprocessorPragmaStatement result = new ASTPragma(); - ((ASTNode) result).setOffsetAndLength(pragma.context_directive_start, - pragma.context_directive_end - pragma.context_directive_start); + ((ASTNode) result).setOffsetAndLength(pragma.getDirectiveStart(), pragma.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1561,8 +1586,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ private IASTPreprocessorStatement createASTUndef(_Undef undef) { IASTPreprocessorUndefStatement result = new ASTUndef(undef.getName()); - ((ASTNode) result).setOffsetAndLength(undef.context_directive_start, - undef.context_directive_end - undef.context_directive_start); + ((ASTNode) result).setOffsetAndLength(undef.getDirectiveStart(), undef.getDirectiveLength()); result.setParent(rootNode); result.setPropertyInParent(IASTTranslationUnit.PREPROCESSOR_STATEMENT); return result; @@ -1580,8 +1604,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { _Context c = findContextForOffset(offset); if( c == null ) return EMPTY_LOCATION_ARRAY; - int offset1 = offset + length - 1; - if (offset1 < c.context_ends) { + if (offset + length <= c.context_ends) { if (c instanceof _CompositeContext) { _Context[] subz = ((_CompositeContext) c).getSubContexts(); boolean foundNested = findNested( subz, offset, length); @@ -1615,7 +1638,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { if (offset2 < sub.context_directive_start) { right= middle-1; } - else if (offset > sub.context_ends) { + else if (offset >= sub.context_ends) { left= middle+1; } else { @@ -1678,38 +1701,32 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { protected IASTNodeLocation createSoleLocation(_Context c, int offset, int length) { - if (c instanceof _IPreprocessorDirective || c instanceof _Problem ) { + + if (c instanceof _IPreprocessorDirective || c instanceof _Problem ) { if (c.containsInDirective(offset, length)) { _CompositeContext parent = c.parent; - while (!(parent instanceof _CompositeFileContext)) - parent = parent.parent; - _CompositeFileContext fc = (_CompositeFileContext) parent; - return new FileLocation(fc.reader.filename, reconcileOffset(fc, - c, offset), length); + if (parent instanceof _CompositeFileContext) { + _CompositeFileContext fc = (_CompositeFileContext) parent; + return new FileLocation(fc.reader.filename, fc.getOffsetInContext(offset), length); + } } } if (c instanceof _CompositeFileContext) { - return new FileLocation( - ((_CompositeFileContext) c).reader.filename, - reconcileOffset(c, offset), length); + _CompositeFileContext cfc = (_CompositeFileContext) c; + return new FileLocation(cfc.reader.filename, cfc.getOffsetInContext(offset), length); } if (c instanceof _MacroExpansion) { _MacroExpansion expansion = (_MacroExpansion) c; - //first check to see if we are in the directive rather than the expansion - if( c.containsInDirective( offset, length ) ) - { + if (c.containsInDirective(offset, length)) { _CompositeContext parent = c.parent; - while (!(parent instanceof _CompositeFileContext)) - parent = parent.parent; - _CompositeFileContext fc = (_CompositeFileContext) parent; - return new FileLocation(fc.reader.filename, reconcileOffset(fc, - c, offset), length); + if (parent instanceof _CompositeFileContext) { + _CompositeFileContext fc = (_CompositeFileContext) parent; + return new FileLocation(fc.reader.filename, fc.getOffsetInContext(offset), length); + } } - IASTNodeLocation[] locations = createSoleLocationArray(c.parent, - c.context_directive_start, c.context_directive_end - - c.context_directive_start + 1); + IASTNodeLocation[] locations = createSoleLocationArray(c.parent, c.getDirectiveStart(), c.getDirectiveLength()); IASTPreprocessorMacroDefinition definition = null; _MacroDefinition d = (_MacroDefinition) expansion.definition; if (d.astNode != null) @@ -1720,8 +1737,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { definition = astNode; } - return new MacroExpansionLocation(definition, locations, - reconcileOffset(c, offset), length); + return new MacroExpansionLocation(definition, locations, expansion.getOffsetInContext(offset), length); } return null; } @@ -1743,58 +1759,6 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { return result; } - /** - * @param fc - * @param c - * @param offset - * @return - */ - protected int reconcileOffset(_CompositeFileContext fc, _Context c, - int offset) { - int subtractOff = 0; - if (c.parent == fc) { - _Context[] subs = fc.getSubContexts(); - for (int i = 0; i < subs.length; ++i) { - _Context sample = subs[i]; - if (sample == c) - break; - if (!(sample instanceof _CompositeContext)) - continue; - subtractOff += sample.context_ends - - sample.context_directive_end; - } - } - return offset - fc.context_directive_end - subtractOff; - } - - /** - * @param c - * @param offset - * @return - */ - protected static int reconcileOffset(_Context c, int offset) { - int subtractOff = 0; - if (c instanceof _CompositeFileContext) { - _Context[] subs = ((_CompositeFileContext) c).getSubContexts(); - for (int i = 0; i < subs.length; ++i) { - _Context subC = subs[i]; - if (subC.context_ends > offset) - break; - if (!(subC instanceof _CompositeContext)) - continue; - - subtractOff += subC.context_ends - subC.context_directive_end; - - } - } - final int result = offset - c.context_directive_end - subtractOff; - return ((result < 0) ? 0 : result); - } - - /** - * @param offset - * @return - */ protected _Context findContextForOffset(int offset) { return tu.findContextContainingOffset(offset); } @@ -2126,9 +2090,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { */ public void encounterProblem(IASTProblem problem) { ScannerASTProblem p = (ScannerASTProblem) problem; - _Problem pr = new _Problem(currentContext, p.getOffset(), p.getOffset() - + p.getLength(), problem); - pr.context_ends = p.getOffset() + p.getLength(); + int offset= p.getOffset(); + int endoffset= offset + p.getLength(); + _Problem pr = new _Problem(currentContext, offset, endoffset, problem); currentContext.addSubContext(pr); } @@ -2310,46 +2274,45 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { if (!(startContext instanceof _CompositeContext)) throw new InvalidPreprocessorNodeException(NOT_VALID_MACRO, globalOffset); - _Context[] contexts = ((_CompositeContext) startContext) - .getSubContexts(); + _Context[] contexts = ((_CompositeContext) startContext).getSubContexts(); // check if a macro in the location map is the selection for (int i = 0; result == null && i < contexts.length; i++) { _Context context = contexts[i]; - // if offset is past the _Context then increment globalOffset - if (globalOffset > context.context_directive_end) { - globalOffset += context.context_ends - - context.context_directive_end; + if (globalOffset < context.getDirectiveStart()) { + break; } - + // check if the _Context is the selection - if (globalOffset == context.context_directive_start - && length == context.context_directive_end - - context.context_directive_start) { - result = createPreprocessorStatement(context); - } - else if( context instanceof _MacroExpansion && globalOffset == context.context_directive_start ) - { - _MacroExpansion expansion = (_MacroExpansion)context; - if( expansion.definition.getName().length == length ) - result = expansion.getName(); + if (globalOffset == context.getDirectiveStart()) { + if( context instanceof _MacroExpansion) { + _MacroExpansion expansion = (_MacroExpansion)context; + if( expansion.definition.getName().length == length ) + result = expansion.getName(); + } + else if (length == context.getDirectiveLength()) { + result = createPreprocessorStatement(context); + } } // check if a sub node of the macro is the selection // TODO // determine how this can be kept in sync with logic in // getAllPreprocessorStatements (i.e. 1:1 mapping) - if ((globalOffset >= context.context_directive_start && globalOffset <= context.context_ends) && context instanceof _MacroDefinition) { - if (globalOffset == ((_MacroDefinition) context).nameOffset - && length == ((_MacroDefinition) context).name.length) - result = createASTMacroDefinition( - (_MacroDefinition) context).getName(); + if (context.containsInDirective(globalOffset, length)) { + if (context instanceof _MacroDefinition) { + _MacroDefinition macroDefinition = (_MacroDefinition) context; + if (globalOffset == macroDefinition.nameOffset + && length == macroDefinition.name.length) { + result = createASTMacroDefinition(macroDefinition).getName(); + } + } } - // stop searching the _Contexts if they've gone past the selection - if (globalOffset < context.context_directive_end) - break; - + // if offset is past the _Context then increment globalOffset + if (globalOffset >= context.getContextStart()) { + globalOffset += context.getContextLength(); + } } return new ASTPreprocessorSelectionResult(result, globalOffset); @@ -2375,16 +2338,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { foundContext = findInclusion(tu, path); if (foundContext == null) { - throw new InvalidPreprocessorNodeException( - TU_INCLUDE_NOT_FOUND, globalOffset); + throw new InvalidPreprocessorNodeException(TU_INCLUDE_NOT_FOUND, globalOffset); } else if (foundContext instanceof _Inclusion) { - globalOffset = foundContext.context_directive_end + offset; // start - // at - // #include's - // directive_end - // + - // real - // offset + globalOffset = foundContext.getContextStart() + offset; } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java index 2365926814e..9767712cf57 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java @@ -193,8 +193,8 @@ public class Scanner2 extends BaseScanner { || bufferData[mostRelevant] instanceof CodeReader) break; MacroData data = (MacroData) bufferData[mostRelevant + 1]; - return new SimpleExpansionToken(signal, data.startOffset, - data.endOffset - data.startOffset + 1, + return new SimpleExpansionToken(signal, data.getStartOffset(), + data.getLength(), getCurrentFilename(), getLineNumber(bufferPos[mostRelevant] + 1)); } @@ -211,18 +211,14 @@ public class Scanner2 extends BaseScanner { || bufferData[mostRelevant] instanceof CodeReader) break; MacroData data = (MacroData) bufferData[mostRelevant + 1]; - return new ImagedExpansionToken(signal, buffer, data.startOffset, - data.endOffset - data.startOffset + 1, + return new ImagedExpansionToken(signal, buffer, data.getStartOffset(), + data.getLength(), getCurrentFilename(), getLineNumber(bufferPos[mostRelevant] + 1)); } IToken i = new ImagedToken(signal, buffer, bufferPos[bufferStackPos] + 1, getCurrentFilename(), getLineNumber(bufferPos[bufferStackPos] + 1)); - if (buffer != null && buffer.length == 0 && signal != IToken.tSTRING - && signal != IToken.tLSTRING) - bufferPos[bufferStackPos] += 1; // TODO - remove this hack at some - // point return i; }