mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52: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,12 +2504,70 @@ public class Scanner2Test extends BaseScanner2Test
|
||||||
validateIdentifier("a");
|
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
|
// 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();
|
StringBuffer buffer = new StringBuffer();
|
||||||
buffer
|
buffer
|
||||||
.append("#ifdef _bug_182180_\n")
|
.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("#endif\n")
|
||||||
.append("bug182180\n");
|
.append("bug182180\n");
|
||||||
initializeScanner(buffer.toString());
|
initializeScanner(buffer.toString());
|
||||||
|
|
|
@ -2338,10 +2338,12 @@ abstract class BaseScanner implements IScanner {
|
||||||
Arrays.fill(result, ' ');
|
Arrays.fill(result, ' ');
|
||||||
int resultCount = 0;
|
int resultCount = 0;
|
||||||
boolean insideString= false;
|
boolean insideString= false;
|
||||||
boolean backslash= false;
|
boolean insideSingleQuote= false;
|
||||||
|
boolean escaped= false;
|
||||||
// either a single-line or multi-line comment was found
|
// either a single-line or multi-line comment was found
|
||||||
for (int i = 0; i < text.length; ++i) {
|
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] == '/') {
|
if (text[i + 1] == '/') {
|
||||||
// done
|
// done
|
||||||
break;
|
break;
|
||||||
|
@ -2353,13 +2355,20 @@ abstract class BaseScanner implements IScanner {
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
} else {
|
} else {
|
||||||
if (insideString && !backslash && text[i] == '\\') {
|
switch (text[i]) {
|
||||||
backslash= true;
|
case '\\':
|
||||||
} else {
|
escaped = !escaped;
|
||||||
backslash= false;
|
break;
|
||||||
}
|
case '"':
|
||||||
if (!backslash && text[i] == '"') {
|
if (!insideSingleQuote && !escaped) {
|
||||||
insideString= !insideString;
|
insideString= !insideString;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '\'':
|
||||||
|
if (!insideString && !escaped) {
|
||||||
|
insideSingleQuote= !insideSingleQuote;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
result[resultCount++] = text[i];
|
result[resultCount++] = text[i];
|
||||||
}
|
}
|
||||||
|
@ -3003,11 +3012,12 @@ abstract class BaseScanner implements IScanner {
|
||||||
|
|
||||||
boolean escaped = false;
|
boolean escaped = false;
|
||||||
boolean insideString= false;
|
boolean insideString= false;
|
||||||
|
boolean insideSingleQuote= false;
|
||||||
while (++pos < limit) {
|
while (++pos < limit) {
|
||||||
char ch= buffer[pos];
|
char ch= buffer[pos];
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '/':
|
case '/':
|
||||||
if (insideComment || insideString) {
|
if (insideComment || insideString || insideSingleQuote) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (pos + 1 < limit) {
|
if (pos + 1 < limit) {
|
||||||
|
@ -3032,10 +3042,15 @@ abstract class BaseScanner implements IScanner {
|
||||||
escaped = !escaped;
|
escaped = !escaped;
|
||||||
continue;
|
continue;
|
||||||
case '"':
|
case '"':
|
||||||
if (!insideComment && !escaped) {
|
if (!insideComment && !insideSingleQuote && !escaped) {
|
||||||
insideString= !insideString;
|
insideString= !insideString;
|
||||||
}
|
}
|
||||||
break;
|
continue;
|
||||||
|
case '\'':
|
||||||
|
if (!insideComment && !insideString && !escaped) {
|
||||||
|
insideSingleQuote= !insideSingleQuote;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
case '\n':
|
case '\n':
|
||||||
if (escaped) {
|
if (escaped) {
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Reference in a new issue