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:
parent
cc1ce25579
commit
0f94639612
2 changed files with 63 additions and 50 deletions
|
@ -7,6 +7,7 @@
|
|||
*
|
||||
* Contributors:
|
||||
* IBM Rational Software - Initial API and implementation
|
||||
* Cheong, Jeong-Sik - fix for 162381
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.core.parser;
|
||||
|
||||
|
@ -100,10 +101,15 @@ public class CodeReader {
|
|||
|
||||
|
||||
CharBuffer charBuffer = Charset.forName(encoding).decode(byteBuffer);
|
||||
if (charBuffer.hasArray())
|
||||
return charBuffer.array();
|
||||
// Got to copy it out
|
||||
char[] buff = new char[charBuffer.length()];
|
||||
char[] buff= null;
|
||||
if (charBuffer.hasArray()) {
|
||||
buff= charBuffer.array();
|
||||
// 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);
|
||||
return buff;
|
||||
}
|
||||
|
|
|
@ -914,8 +914,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
}
|
||||
|
||||
public int context_directive_start;
|
||||
public int context_directive_end;
|
||||
public int context_ends;
|
||||
public int context_directive_end; // inclusive
|
||||
public int context_ends; // inclusive
|
||||
public _CompositeContext parent;
|
||||
|
||||
public _CompositeContext getParent() {
|
||||
|
@ -971,16 +971,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
public _Context[] getSubContexts() {
|
||||
if (subContexts == null)
|
||||
return EMPTY_CONTEXT_ARRAY;
|
||||
trimSubContexts();
|
||||
return subContexts;
|
||||
final int length= subContexts.length;
|
||||
if (length > 0 && subContexts[length-1] == null) {
|
||||
subContexts = (_Context[]) ArrayUtil.trim(_Context.class, subContexts);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void trimSubContexts() {
|
||||
subContexts = (_Context[]) ArrayUtil.trim(_Context.class,
|
||||
subContexts);
|
||||
return subContexts;
|
||||
}
|
||||
|
||||
public void addSubContext(_Context c) {
|
||||
|
@ -990,19 +985,6 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
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() {
|
||||
if (subContexts == null)
|
||||
return false;
|
||||
|
@ -1015,16 +997,25 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
public _Context findContextContainingOffset(int offset) {
|
||||
if (subContexts != null)
|
||||
{
|
||||
for (int i = 0; i < subContexts.length; ++i) {
|
||||
_Context c = subContexts[i];
|
||||
if (c == null) continue;
|
||||
if ((offset >= c.context_directive_start && offset <= c.context_ends)) {
|
||||
int left = 0;
|
||||
int right = subContexts.length - 1;
|
||||
int middle = 0;
|
||||
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) {
|
||||
_Context trial = ((_CompositeContext) c)
|
||||
.findContextContainingOffset(offset);
|
||||
if (trial == null)
|
||||
return c;
|
||||
return trial;
|
||||
final _CompositeContext compositeContext = (_CompositeContext) c;
|
||||
_Context nested= compositeContext.findContextContainingOffset(offset);
|
||||
if (nested != null) {
|
||||
return nested;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
@ -1591,19 +1582,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
_Context c = findContextForOffset(offset);
|
||||
if( c == null )
|
||||
return EMPTY_LOCATION_ARRAY;
|
||||
int offset1 = offset + length;
|
||||
if ((offset1 >= c.context_directive_start && offset1 <= c.context_ends)) {
|
||||
int offset1 = offset + length - 1;
|
||||
if (offset1 < c.context_ends) {
|
||||
if (c instanceof _CompositeContext) {
|
||||
_Context[] subz = ((_CompositeContext) c).getSubContexts();
|
||||
boolean foundNested = false;
|
||||
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;
|
||||
}
|
||||
}
|
||||
boolean foundNested = findNested( subz, offset, length);
|
||||
if (!foundNested)
|
||||
return createSoleLocationArray(c, offset, length);
|
||||
} else
|
||||
|
@ -1621,6 +1604,30 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
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,
|
||||
_WeightedContext[] contexts) {
|
||||
IASTNodeLocation[] result = new IASTNodeLocation[contexts.length];
|
||||
|
|
Loading…
Add table
Reference in a new issue