1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-09 09:15:38 +02:00

Fix for 183930, by Emanuel Graf, comments from inactive code.

This commit is contained in:
Markus Schorn 2007-05-02 12:37:08 +00:00
parent 0a81ce7c3d
commit 904a9560e3
2 changed files with 82 additions and 4 deletions

View file

@ -199,13 +199,13 @@ public class CommentTests extends AST2BaseTest {
// #else // #else
// // comment2 // // comment2
// #endif // #endif
public void _testCommentsInInactiveCode_bug183930() throws Exception { public void testCommentsInInactiveCode_bug183930() throws Exception {
StringBuffer code= getContents(1)[0]; StringBuffer code= getContents(1)[0];
IASTTranslationUnit tu = parse(code.toString(), ParserLanguage.CPP, false, true, true); IASTTranslationUnit tu = parse(code.toString(), ParserLanguage.CPP, false, true, true);
IASTComment[] comments = tu.getComments(); IASTComment[] comments = tu.getComments();
assertEquals(2, comments.length); assertEquals(2, comments.length);
assertEquals("// comment1", new String(comments[0].getComment())); assertEquals("// comment1", new String(comments[0].getComment()));
assertEquals("// comment2", new String(comments[0].getComment())); assertEquals("// comment2", new String(comments[1].getComment()));
} }
} }

View file

@ -42,6 +42,7 @@ import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode; import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode; import org.eclipse.cdt.core.parser.ast.IASTCompletionNode;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayIntMap; import org.eclipse.cdt.core.parser.util.CharArrayIntMap;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArraySet; import org.eclipse.cdt.core.parser.util.CharArraySet;
@ -198,6 +199,8 @@ abstract class BaseScanner implements IScanner {
protected boolean scanComments; protected boolean scanComments;
protected IToken[] commentsFromInactiveCode = new IToken[0];
protected final CharArrayIntMap additionalKeywords; protected final CharArrayIntMap additionalKeywords;
protected final CharArrayIntMap additionalPPKeywords; protected final CharArrayIntMap additionalPPKeywords;
@ -662,6 +665,13 @@ abstract class BaseScanner implements IScanner {
throw new ParseError( throw new ParseError(
ParseError.ParseErrorKind.TIMEOUT_OR_CANCELLED); ParseError.ParseErrorKind.TIMEOUT_OR_CANCELLED);
//return the stored comments
if(commentsFromInactiveCode.length > 0 && commentsFromInactiveCode[0] != null){
IToken commentToken = commentsFromInactiveCode[0];
ArrayUtil.remove(commentsFromInactiveCode, commentToken);
return commentToken;
}
// Find the first thing we would care about // Find the first thing we would care about
skipOverWhiteSpaceFetchToken(); skipOverWhiteSpaceFetchToken();
@ -2544,7 +2554,7 @@ abstract class BaseScanner implements IScanner {
while (bufferPos[bufferStackPos] < limit) { while (bufferPos[bufferStackPos] < limit) {
skipOverWhiteSpace(); skipOverWhiteSpaceFetchToken();
if (++bufferPos[bufferStackPos] >= limit) if (++bufferPos[bufferStackPos] >= limit)
return; return;
@ -2675,9 +2685,13 @@ abstract class BaseScanner implements IScanner {
} }
} }
} else if (c != '\n') } else if (c != '\n')
if(scanComments){
skipToNewLineAndCollectComments();
}else{
skipToNewLine(); skipToNewLine();
} }
} }
}
protected boolean skipOverWhiteSpace() { protected boolean skipOverWhiteSpace() {
return skipOverWhiteSpaceAndParseComments(); return skipOverWhiteSpaceAndParseComments();
@ -3170,6 +3184,70 @@ abstract class BaseScanner implements IScanner {
bufferPos[bufferStackPos]= pos-1; bufferPos[bufferStackPos]= pos-1;
} }
protected void skipToNewLineAndCollectComments() {
char[] buffer = bufferStack[bufferStackPos];
int limit = bufferLimit[bufferStackPos];
int pos = bufferPos[bufferStackPos];
boolean escaped = false;
boolean insideString= false;
boolean insideSingleQuote= false;
for (;pos < limit;++pos) {
char ch= buffer[pos];
switch (ch) {
case '/':
if (insideString || insideSingleQuote) {
break;
}
if (pos + 1 < limit) {
char c= buffer[pos + 1];
if (c == '*'||c == '/') {
bufferPos[bufferStackPos] = pos;
IToken comment = scanComment();
commentsFromInactiveCode = (IToken[]) ArrayUtil.append(comment.getClass(), commentsFromInactiveCode, comment);
pos = bufferPos[bufferStackPos];
}
}
break;
case '\\':
escaped = !escaped;
continue;
case '"':
if (!insideSingleQuote) {
insideString= insideString ? escaped : true;
}
break;
case '\'':
if (!insideString) {
insideSingleQuote= insideSingleQuote ? escaped : true;
}
break;
case '\n':
if (escaped) {
break;
}
bufferPos[bufferStackPos]= pos;
return;
case '\r':
if (pos+1 < limit && buffer[pos+1] == '\n') {
if (escaped) {
pos++;
break;
}
bufferPos[bufferStackPos]= pos;
return;
}
break;
default:
break;
}
escaped = false;
}
bufferPos[bufferStackPos]= pos;
}
protected char[] handleFunctionStyleMacro(FunctionStyleMacro macro, protected char[] handleFunctionStyleMacro(FunctionStyleMacro macro,
boolean pushContext) { boolean pushContext) {
char[] buffer = bufferStack[bufferStackPos]; char[] buffer = bufferStack[bufferStackPos];