1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 23:35:48 +02:00

Patch for Devin Steffler.

Bug 86870	  	[Offsets] simple #if/#define example seems flaky with offset/length
This commit is contained in:
John Camelon 2005-05-25 14:57:08 +00:00
parent 98fa5f404a
commit 4f2422d695
2 changed files with 59 additions and 9 deletions

View file

@ -37,6 +37,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/** /**
* @author dsteffle * @author dsteffle
@ -1510,6 +1511,54 @@ public class AST2SelectionParseTest extends AST2SelectionParseBaseTest {
assertTrue(name.resolveBinding() instanceof IVariable); assertTrue(name.resolveBinding() instanceof IVariable);
} }
public void testBug86870() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("#if VERSION == 1\r\n"); //$NON-NLS-1$
buffer.append("#define INCFILE \"vers1.h\"\r\n"); //$NON-NLS-1$
buffer.append("#elif VERSION == 2\r\n"); //$NON-NLS-1$
buffer.append("#define INCFILE \"vers2.h\" /* and so on */\r\n"); //$NON-NLS-1$
buffer.append("#else\r\n"); //$NON-NLS-1$
buffer.append("#define INCFILE \"versN.h\"\r\n"); //$NON-NLS-1$
buffer.append("#endif\r\n"); //$NON-NLS-1$
String code = buffer.toString();
int offset1 = code.indexOf( "#if VERSION == 1" ); //$NON-NLS-1$
int length = "#if VERSION == 1".length(); //$NON-NLS-1$
IASTNode node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
assertEquals( ((ASTNode)node).getLength(), length);
offset1 = code.indexOf( "#elif VERSION == 2" ); //$NON-NLS-1$
length = "#elif VERSION == 2".length(); //$NON-NLS-1$
node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
assertEquals( ((ASTNode)node).getLength(), length);
offset1 = code.indexOf( "#else" ); //$NON-NLS-1$
length = "#else".length(); //$NON-NLS-1$
node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
assertEquals( ((ASTNode)node).getLength(), length);
offset1 = code.indexOf( "#define INCFILE \"versN.h\"" ); //$NON-NLS-1$
length = "#define INCFILE \"versN.h\"".length(); //$NON-NLS-1$
node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
assertEquals( ((ASTNode)node).getLength(), length);
offset1 = code.indexOf( "INCFILE \"versN.h\"" ); //$NON-NLS-1$
length = "INCFILE".length(); //$NON-NLS-1$
node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
assertEquals( ((ASTNode)node).getLength(), length);
offset1 = code.indexOf( "#endif" ); //$NON-NLS-1$
length = "#endif".length(); //$NON-NLS-1$
node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
assertEquals( ((ASTNode)node).getLength(), length);
}
public void testBug87179() throws Exception public void testBug87179() throws Exception
{ {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();

View file

@ -2583,6 +2583,9 @@ abstract class BaseScanner implements IScanner {
handleCompletionOnExpression(CharArrayUtils.extract( handleCompletionOnExpression(CharArrayUtils.extract(
buffer, start, len)); buffer, start, len));
branchState(BRANCH_IF); branchState(BRANCH_IF);
processIf(pos, pos + start + len, true); // fix 86870, process IF before skipping the condition and only process it once
if (expressionEvaluator.evaluate(buffer, start, len, if (expressionEvaluator.evaluate(buffer, start, len,
definitions, definitions,
getLineNumber(bufferPos[bufferStackPos]), getLineNumber(bufferPos[bufferStackPos]),
@ -2590,9 +2593,7 @@ abstract class BaseScanner implements IScanner {
skipOverConditionalCode(true); skipOverConditionalCode(true);
if (isLimitReached()) if (isLimitReached())
handleInvalidCompletion(); handleInvalidCompletion();
processIf(pos, bufferPos[bufferStackPos], true);
} }
processIf(pos, bufferPos[bufferStackPos], false);
return; return;
case ppElse: case ppElse:
case ppElif: case ppElif:
@ -3406,11 +3407,11 @@ abstract class BaseScanner implements IScanner {
skipToNewLine(); skipToNewLine();
if (checkelse && nesting == 0) { if (checkelse && nesting == 0) {
processElse(startPos, processElse(startPos,
bufferPos[bufferStackPos] + 1, true); bufferPos[bufferStackPos], true);
return; return;
} }
processElse(startPos, processElse(startPos,
bufferPos[bufferStackPos] + 1, false); bufferPos[bufferStackPos], false);
} else { } else {
//problem, ignore this one. //problem, ignore this one.
handleProblem( handleProblem(
@ -3436,15 +3437,15 @@ abstract class BaseScanner implements IScanner {
getLineNumber(bufferPos[bufferStackPos]), getLineNumber(bufferPos[bufferStackPos]),
getCurrentFilename()) != 0) { getCurrentFilename()) != 0) {
// condition passed, we're good // condition passed, we're good
processElsif(start, processElsif(startPos,
bufferPos[bufferStackPos], true); bufferPos[bufferStackPos], true);
return; return;
} }
processElsif(start, processElsif(startPos,
bufferPos[bufferStackPos], false); bufferPos[bufferStackPos], false);
} else { } else {
skipToNewLine(); skipToNewLine();
processElsif(start, processElsif(startPos,
bufferPos[bufferStackPos], false); bufferPos[bufferStackPos], false);
} }
} else { } else {
@ -3890,7 +3891,7 @@ abstract class BaseScanner implements IScanner {
break; break;
} else if (!escaped && bufferPos[bufferStackPos] < limit } else if (!escaped && bufferPos[bufferStackPos] < limit
&& buffer[bufferPos[bufferStackPos] + 1] == '\n') { && buffer[bufferPos[bufferStackPos] + 1] == '\n') {
bufferPos[bufferStackPos]++; // bufferPos[bufferStackPos]++; // Do not want to skip past the \r
return; return;
} }
break; break;