1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fix for 162381, by Jeong-Sik Cheong, null characters in code reader.

This commit is contained in:
Markus Schorn 2006-10-30 15:46:06 +00:00
parent cc1ce25579
commit 0f94639612
2 changed files with 63 additions and 50 deletions

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* IBM Rational Software - Initial API and implementation * IBM Rational Software - Initial API and implementation
* Cheong, Jeong-Sik - fix for 162381
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.parser; package org.eclipse.cdt.core.parser;
@ -100,10 +101,15 @@ public class CodeReader {
CharBuffer charBuffer = Charset.forName(encoding).decode(byteBuffer); CharBuffer charBuffer = Charset.forName(encoding).decode(byteBuffer);
if (charBuffer.hasArray()) char[] buff= null;
return charBuffer.array(); if (charBuffer.hasArray()) {
// Got to copy it out buff= charBuffer.array();
char[] buff = new char[charBuffer.length()]; // bug 162381, handle the case where the buffer is longer
if (buff.length == charBuffer.length()) {
return buff;
}
}
buff = new char[charBuffer.length()];
charBuffer.get(buff); charBuffer.get(buff);
return buff; return buff;
} }

View file

@ -914,8 +914,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
} }
public int context_directive_start; public int context_directive_start;
public int context_directive_end; public int context_directive_end; // inclusive
public int context_ends; public int context_ends; // inclusive
public _CompositeContext parent; public _CompositeContext parent;
public _CompositeContext getParent() { public _CompositeContext getParent() {
@ -971,16 +971,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
public _Context[] getSubContexts() { public _Context[] getSubContexts() {
if (subContexts == null) if (subContexts == null)
return EMPTY_CONTEXT_ARRAY; return EMPTY_CONTEXT_ARRAY;
trimSubContexts(); final int length= subContexts.length;
return subContexts; if (length > 0 && subContexts[length-1] == null) {
subContexts = (_Context[]) ArrayUtil.trim(_Context.class, subContexts);
} }
return subContexts;
/**
*
*/
private void trimSubContexts() {
subContexts = (_Context[]) ArrayUtil.trim(_Context.class,
subContexts);
} }
public void addSubContext(_Context c) { public void addSubContext(_Context c) {
@ -990,19 +985,6 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
subContexts, c); subContexts, c);
} }
public void removeSubContext(_Context c) {
if (subContexts == null)
return;
for (int i = 0; i < subContexts.length; ++i)
if (subContexts[i] == c) {
subContexts[i] = null;
return;
}
}
/**
* @return
*/
public boolean hasSubContexts() { public boolean hasSubContexts() {
if (subContexts == null) if (subContexts == null)
return false; return false;
@ -1015,16 +997,25 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
public _Context findContextContainingOffset(int offset) { public _Context findContextContainingOffset(int offset) {
if (subContexts != null) if (subContexts != null)
{ {
for (int i = 0; i < subContexts.length; ++i) { int left = 0;
_Context c = subContexts[i]; int right = subContexts.length - 1;
if (c == null) continue; int middle = 0;
if ((offset >= c.context_directive_start && offset <= c.context_ends)) { while (left <= right) {
middle = (left+right) / 2;
_Context c = subContexts[middle];
if (c== null || offset < c.context_directive_start) {
right= middle-1;
}
else if (offset > c.context_ends) {
left= middle+1;
}
else {
if (c instanceof _CompositeContext) { if (c instanceof _CompositeContext) {
_Context trial = ((_CompositeContext) c) final _CompositeContext compositeContext = (_CompositeContext) c;
.findContextContainingOffset(offset); _Context nested= compositeContext.findContextContainingOffset(offset);
if (trial == null) if (nested != null) {
return c; return nested;
return trial; }
} }
return c; return c;
} }
@ -1591,19 +1582,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
_Context c = findContextForOffset(offset); _Context c = findContextForOffset(offset);
if( c == null ) if( c == null )
return EMPTY_LOCATION_ARRAY; return EMPTY_LOCATION_ARRAY;
int offset1 = offset + length; int offset1 = offset + length - 1;
if ((offset1 >= c.context_directive_start && offset1 <= c.context_ends)) { if (offset1 < c.context_ends) {
if (c instanceof _CompositeContext) { if (c instanceof _CompositeContext) {
_Context[] subz = ((_CompositeContext) c).getSubContexts(); _Context[] subz = ((_CompositeContext) c).getSubContexts();
boolean foundNested = false; boolean foundNested = findNested( subz, offset, length);
for (int i = 0; i < subz.length; ++i) {
_Context sub = subz[i];
if (sub.context_directive_start > offset
&& sub.context_ends <= offset + length) {
foundNested = true;
break;
}
}
if (!foundNested) if (!foundNested)
return createSoleLocationArray(c, offset, length); return createSoleLocationArray(c, offset, length);
} else } else
@ -1621,6 +1604,30 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
return createMegaLocationArray(offset, length, extraContexts); return createMegaLocationArray(offset, length, extraContexts);
} }
private boolean findNested(_Context[] subz, int offset, int length){
final int offset2= offset+length-1;
int left = 0;
int right = subz.length-1;
int middle = 0;
_Context sub = null;
while( left <= right ){
middle = (left+right)/2;
sub = subz[middle];
if (offset2 < sub.context_directive_start) {
right= middle-1;
}
else if (offset > sub.context_ends) {
left= middle+1;
}
else {
return true;
}
}
return false;
}
private IASTNodeLocation[] createMegaLocationArray(int offset, int length, private IASTNodeLocation[] createMegaLocationArray(int offset, int length,
_WeightedContext[] contexts) { _WeightedContext[] contexts) {
IASTNodeLocation[] result = new IASTNodeLocation[contexts.length]; IASTNodeLocation[] result = new IASTNodeLocation[contexts.length];