From b4d5bb7a677e662fd93e410414c42b0e948a8f66 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 5 Mar 2008 12:41:59 +0000 Subject: [PATCH] Testcases and fixes for selecting nodes by file offsets. --- .../tests/ast2/ASTNodeSelectorTest.java | 62 ++++++++++++++++++- .../core/dom/parser/ASTNodeSelector.java | 3 + .../core/dom/parser/ASTNodeSpecification.java | 6 +- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ASTNodeSelectorTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ASTNodeSelectorTest.java index 7b456299882..5214f4bc272 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ASTNodeSelectorTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ASTNodeSelectorTest.java @@ -354,7 +354,7 @@ public class ASTNodeSelectorTest extends AST2BaseTest { int x1= fCode.indexOf("EXPLICIT;"); testContainedName(x1, fCode.length(), "EXPLICIT"); testName(x1, x1+8, "EXPLICIT"); - testEnclosingName(x1, 0, "EXPLICIT"); + testEnclosingName(x1, x1, "EXPLICIT"); } // #define shift_offsets @@ -367,6 +367,64 @@ public class ASTNodeSelectorTest extends AST2BaseTest { int x1= fCode.indexOf("NESTED)"); testContainedName(x1, fCode.length(), "NESTED"); testName(x1, x1+6, "NESTED"); - testEnclosingName(x1, 0, "NESTED"); + testEnclosingName(x1, x1, "NESTED"); } + + // #define id(x,y) x y + // id(int a, =1); + // id(int b=, a); + public void testImageLocations() { + int a1= fCode.indexOf("a"); + int a2= fCode.indexOf("a", a1+1); + int b1= fCode.indexOf("b"); + + testName(a1, a1+1, "a"); + testContainedName(a1-1, a2+2, "a"); + testEnclosingName(a1, a1, "a"); + testEnclosingName(a1+1, a1+1, "a"); + + testName(a2, a2+1, "a"); + testContainedName(a2-1, a2+2, "a"); + testEnclosingName(a2, a2, "a"); + testEnclosingName(a2+1, a2+1, "a"); + + testEnclosingNode(a1-1, a1+1, "id(int a, =1)"); + testContainedNode(a1-8, a1+1, "id"); + } + + // namespace ns {int a;} + // int x= ns::a; + // #define M int b; + // M + // #define N() int c; + // N() + // #define O + // #define P() + // P()O + public void testOrdering() { + int x1= fCode.indexOf("ns::a"); + int x2= x1 + "ns::a".length(); + testContainedName(x1, x2, "ns"); + testEnclosingName(x2-1, x2-1, "a"); + testEnclosingName(x2, x2, "a"); + + x1= fCode.indexOf("M"); x1= fCode.indexOf("M", x1+1); + testNode(x1, x1+1, "M"); + testEnclosingNode(x1, x1, "M"); + testEnclosingNode(x1+1, x1+1, "M"); + testContainedNode(x1-1, x1+2, "M"); + + x1= fCode.indexOf("N"); x1= fCode.indexOf("N", x1+1); + testNode(x1, x1+1, "N"); + testEnclosingNode(x1, x1, "N"); + testEnclosingNode(x1+1, x1+1, "N"); + testContainedNode(x1-1, x1+2, "N"); + + x1= fCode.indexOf("O"); x1= fCode.indexOf("O", x1+1); + testNode(x1, x1+1, "O"); + testEnclosingNode(x1, x1, "O"); + testEnclosingNode(x1+1, x1+1, "O"); + testContainedNode(x1-1, x1+2, "O"); + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSelector.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSelector.java index 8dc40246ec0..f41a78e031e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSelector.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSelector.java @@ -52,6 +52,9 @@ public class ASTNodeSelector implements IASTNodeSelector { if (!fIsValid) { return null; } + if (lengthInFile < 0) { + throw new IllegalArgumentException("Length cannot be less than zero."); //$NON-NLS-1$ + } int sequenceLength; int altSequenceNumber= -1; int sequenceNumber= fLocationResolver.getSequenceNumberForFileOffset(fFilePath, offsetInFile); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSpecification.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSpecification.java index 821ca3e595c..d0ba18aa31b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSpecification.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNodeSpecification.java @@ -144,7 +144,7 @@ public class ASTNodeSpecification { final int endOffset= offset+length; switch(fRelation) { case EXACT_MATCH: - return !isParent(cand, fBestNode); + return isParent(fBestNode, cand); case FIRST_CONTAINED: if (offset < fBestOffset) { return true; @@ -153,7 +153,7 @@ public class ASTNodeSpecification { if (endOffset < fBestEndOffset) { return true; } - return endOffset == fBestEndOffset && !isParent(cand, fBestNode); + return endOffset == fBestEndOffset && isParent(fBestNode, cand); } return false; case ENCLOSING: @@ -161,7 +161,7 @@ public class ASTNodeSpecification { if (length < bestLength) { return true; } - return length == bestLength && !isParent(cand, fBestNode); + return length == bestLength && isParent(fBestNode, cand); default: assert false; return false;