mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Fix for 182180: Bug with parser and #ifdef statements and "/*" (follow-up)
This commit is contained in:
parent
df81aa5942
commit
dae80ef2cd
2 changed files with 88 additions and 15 deletions
|
@ -2504,16 +2504,74 @@ public class Scanner2Test extends BaseScanner2Test
|
|||
validateIdentifier("a");
|
||||
}
|
||||
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=180172
|
||||
public void testBug180172() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
String value= "\"https://bugs.eclipse.org/bugs/show_bug.cgi?id=180172\"";
|
||||
buffer.append("#define bug180172 ").append(value).append(" // bla \n");
|
||||
initializeScanner(buffer.toString());
|
||||
fullyTokenize();
|
||||
validateDefinition("bug180172", value);
|
||||
}
|
||||
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180
|
||||
public void testBug182180() throws Exception {
|
||||
public void testBug182180_1() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer
|
||||
.append("#ifdef _bug_182180_\n")
|
||||
.append(" printf(\"Hello World /*.ap\\n\");\n")
|
||||
.append("printf(\"Hello World /*.ap\\n\");\n")
|
||||
.append("#endif\n")
|
||||
.append("bug182180\n");
|
||||
initializeScanner(buffer.toString());
|
||||
validateIdentifier("bug182180");
|
||||
}
|
||||
|
||||
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180
|
||||
public void testBug182180_2() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer
|
||||
.append("#ifdef _bug_182180_\n")
|
||||
.append("char c='\"'; printf(\"Hello World /*.ap\\n\");\n")
|
||||
.append("#endif\n")
|
||||
.append("bug182180\n");
|
||||
initializeScanner(buffer.toString());
|
||||
validateIdentifier("bug182180");
|
||||
}
|
||||
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180
|
||||
public void testBug182180_3() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer
|
||||
.append("#ifdef _bug_182180_\n")
|
||||
.append("char c1='\\'',c2='\\\"'; printf(\"Hello World /*.ap\\n\");\n")
|
||||
.append("#endif\n")
|
||||
.append("bug182180\n");
|
||||
initializeScanner(buffer.toString());
|
||||
validateIdentifier("bug182180");
|
||||
}
|
||||
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180
|
||||
public void testBug182180_4() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer
|
||||
.append("#ifdef _bug_182180_\n")
|
||||
.append("printf(\"Hello '\"'World /*.ap\\n\");\n")
|
||||
.append("#endif\n")
|
||||
.append("bug182180\n");
|
||||
initializeScanner(buffer.toString());
|
||||
validateIdentifier("bug182180");
|
||||
}
|
||||
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180
|
||||
public void testBug182180_5() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer
|
||||
.append("#ifdef _bug_182180_\n")
|
||||
.append("printf(\"Hello \\\"World /*.ap\\n\");\n")
|
||||
.append("#endif\n")
|
||||
.append("bug182180\n");
|
||||
initializeScanner(buffer.toString());
|
||||
validateIdentifier("bug182180");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2338,10 +2338,12 @@ abstract class BaseScanner implements IScanner {
|
|||
Arrays.fill(result, ' ');
|
||||
int resultCount = 0;
|
||||
boolean insideString= false;
|
||||
boolean backslash= false;
|
||||
boolean insideSingleQuote= false;
|
||||
boolean escaped= false;
|
||||
// either a single-line or multi-line comment was found
|
||||
for (int i = 0; i < text.length; ++i) {
|
||||
if (!insideString && (text[i] == '/' && (i + 1 < text.length) && (text[i + 1] == '*' || text[i + 1] == '/'))) {
|
||||
if (!insideString && !insideSingleQuote && (text[i] == '/'
|
||||
&& (i + 1 < text.length) && (text[i + 1] == '*' || text[i + 1] == '/'))) {
|
||||
if (text[i + 1] == '/') {
|
||||
// done
|
||||
break;
|
||||
|
@ -2353,13 +2355,20 @@ abstract class BaseScanner implements IScanner {
|
|||
}
|
||||
++i;
|
||||
} else {
|
||||
if (insideString && !backslash && text[i] == '\\') {
|
||||
backslash= true;
|
||||
} else {
|
||||
backslash= false;
|
||||
}
|
||||
if (!backslash && text[i] == '"') {
|
||||
insideString= !insideString;
|
||||
switch (text[i]) {
|
||||
case '\\':
|
||||
escaped = !escaped;
|
||||
break;
|
||||
case '"':
|
||||
if (!insideSingleQuote && !escaped) {
|
||||
insideString= !insideString;
|
||||
}
|
||||
break;
|
||||
case '\'':
|
||||
if (!insideString && !escaped) {
|
||||
insideSingleQuote= !insideSingleQuote;
|
||||
}
|
||||
break;
|
||||
}
|
||||
result[resultCount++] = text[i];
|
||||
}
|
||||
|
@ -3003,11 +3012,12 @@ abstract class BaseScanner implements IScanner {
|
|||
|
||||
boolean escaped = false;
|
||||
boolean insideString= false;
|
||||
boolean insideSingleQuote= false;
|
||||
while (++pos < limit) {
|
||||
char ch= buffer[pos];
|
||||
switch (ch) {
|
||||
case '/':
|
||||
if (insideComment || insideString) {
|
||||
if (insideComment || insideString || insideSingleQuote) {
|
||||
break;
|
||||
}
|
||||
if (pos + 1 < limit) {
|
||||
|
@ -3032,10 +3042,15 @@ abstract class BaseScanner implements IScanner {
|
|||
escaped = !escaped;
|
||||
continue;
|
||||
case '"':
|
||||
if (!insideComment && !escaped) {
|
||||
if (!insideComment && !insideSingleQuote && !escaped) {
|
||||
insideString= !insideString;
|
||||
}
|
||||
break;
|
||||
continue;
|
||||
case '\'':
|
||||
if (!insideComment && !insideString && !escaped) {
|
||||
insideSingleQuote= !insideSingleQuote;
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
if (escaped) {
|
||||
break;
|
||||
|
|
Loading…
Add table
Reference in a new issue